Davide Dunne
Blazor: CSV exported is empty
Still not working :/
private Stream GetFileStream()
{
var memoryStream = new MemoryStream();
var streamWriter = new StreamWriter(memoryStream);
var csvWriter = new CsvWriter(streamWriter, culture: CultureInfo.InvariantCulture);
csvWriter.WriteRecords(objects);
// Reset memory stream
memoryStream.Position = 0;
return memoryStream;
}
private async Task DownloadFileFromStream()
{
using var fileStream = GetFileStream();
using var streamRef = new DotNetStreamReference(stream: fileStream);
await JS.InvokeVoidAsync("downloadFileFromStream", FileName, streamRef);
}
private Stream GetFileStream()
{
var memoryStream = new MemoryStream();
var streamWriter = new StreamWriter(memoryStream);
var csvWriter = new CsvWriter(streamWriter, culture: CultureInfo.InvariantCulture);
csvWriter.WriteRecords(objects);
// Reset memory stream
memoryStream.Position = 0;
return memoryStream;
}
private async Task DownloadFileFromStream()
{
using var fileStream = GetFileStream();
using var streamRef = new DotNetStreamReference(stream: fileStream);
await JS.InvokeVoidAsync("downloadFileFromStream", FileName, streamRef);
}
8 replies
Blazor: CSV exported is empty
window.downloadFileFromStream = async (fileName, contentStreamReference) => {
const arrayBuffer = await contentStreamReference.arrayBuffer();
const blob = new Blob([arrayBuffer]);
const url = URL.createObjectURL(blob);
const anchorElement = document.createElement('a');
anchorElement.href = url;
anchorElement.download = fileName ?? '';
anchorElement.click();
anchorElement.remove();
URL.revokeObjectURL(url);
}
window.downloadFileFromStream = async (fileName, contentStreamReference) => {
const arrayBuffer = await contentStreamReference.arrayBuffer();
const blob = new Blob([arrayBuffer]);
const url = URL.createObjectURL(blob);
const anchorElement = document.createElement('a');
anchorElement.href = url;
anchorElement.download = fileName ?? '';
anchorElement.click();
anchorElement.remove();
URL.revokeObjectURL(url);
}
8 replies
Blazor: CSV exported is empty
@using System.IO
@using CsvHelper
@using System.Globalization
@inject IJSRuntime JS
@code {
/// <summary>
/// Export to CSV from a Collection by clicking a button
/// Code obtained from: https://learn.microsoft.com/en-us/aspnet/core/blazor/file-downloads?view=aspnetcore-6.0#download-from-a-stream
/// https://joshclose.github.io/CsvHelper/getting-started/#writing-a-csv-file
/// </summary>
[Parameter, EditorRequired]
public IEnumerable<object>? objects { get; set; }
[Parameter, EditorRequired]
public string? FileName { get; set; }
[Parameter, EditorRequired]
public string? TextDisplay { get; set; }
private Stream GetFileStream()
{
var memoryStream = new MemoryStream();
var streamWriter = new StreamWriter(memoryStream);
var csvWriter = new CsvWriter(streamWriter, culture: CultureInfo.InvariantCulture);
csvWriter.WriteRecords(objects);
return memoryStream;
}
private async Task DownloadFileFromStream()
{
var fileStream = GetFileStream();
var streamRef = new DotNetStreamReference(stream: fileStream);
await JS.InvokeVoidAsync("downloadFileFromStream", FileName, streamRef);
}
}
<MudIconButton Icon="@Icons.Material.Filled.Download" Color="Color.Secondary" Size="Size.Small" aria-label="Export to CSV" OnClick="DownloadFileFromStream">@TextDisplay</MudIconButton>
@using System.IO
@using CsvHelper
@using System.Globalization
@inject IJSRuntime JS
@code {
/// <summary>
/// Export to CSV from a Collection by clicking a button
/// Code obtained from: https://learn.microsoft.com/en-us/aspnet/core/blazor/file-downloads?view=aspnetcore-6.0#download-from-a-stream
/// https://joshclose.github.io/CsvHelper/getting-started/#writing-a-csv-file
/// </summary>
[Parameter, EditorRequired]
public IEnumerable<object>? objects { get; set; }
[Parameter, EditorRequired]
public string? FileName { get; set; }
[Parameter, EditorRequired]
public string? TextDisplay { get; set; }
private Stream GetFileStream()
{
var memoryStream = new MemoryStream();
var streamWriter = new StreamWriter(memoryStream);
var csvWriter = new CsvWriter(streamWriter, culture: CultureInfo.InvariantCulture);
csvWriter.WriteRecords(objects);
return memoryStream;
}
private async Task DownloadFileFromStream()
{
var fileStream = GetFileStream();
var streamRef = new DotNetStreamReference(stream: fileStream);
await JS.InvokeVoidAsync("downloadFileFromStream", FileName, streamRef);
}
}
<MudIconButton Icon="@Icons.Material.Filled.Download" Color="Color.Secondary" Size="Size.Small" aria-label="Export to CSV" OnClick="DownloadFileFromStream">@TextDisplay</MudIconButton>
8 replies