Davide Dunne
Davide Dunne
CC#
Created by Davide Dunne on 3/25/2024 in #help
Blazor Server
No description
2 replies
CC#
Created by Davide Dunne on 3/20/2024 in #help
Deploy Blazor Entity Framework code first on IIS
Hello everyone I was asked to deploy a Blazor Application with EF Code First, in order to run the migrations on the server do I have to get all the code and then compile it from the server or can I still run the migration only with the binaries?
3 replies
CC#
Created by Davide Dunne on 2/20/2024 in #help
Blazor: CSV exported is empty
Hi, I'm using CsvHelper to export a list of objects to a csv and then make the use download, the file gets downloaded but it's empty.
8 replies
CC#
Created by Davide Dunne on 2/19/2024 in #help
Blazor Server: Cannot access a closed Stream
I'm trying to export a list of objects to a csv and then make the user download it. I'm getting an error for having the stream closed. If I remove all the using in GetFileStream I end up getting another error for Javascript not found
@using System.IO
@using CsvHelper
@using System.Globalization
@inject IJSRuntime JS
@code {
[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()
{
using var memoryStream = new MemoryStream();
using var streamWriter = new StreamWriter(memoryStream);
using var csvWriter = new CsvWriter(streamWriter, culture: CultureInfo.InvariantCulture);
csvWriter.WriteRecords(objects);
return memoryStream;
}
private async Task DownloadFileFromStream()
{
var fileStream = GetFileStream();
using var streamRef = new DotNetStreamReference(stream: fileStream);
await JS.InvokeVoidAsync("downloadFileFromStream", FileName, streamRef);
}
}
<script>
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);
}
</script>
<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 {
[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()
{
using var memoryStream = new MemoryStream();
using var streamWriter = new StreamWriter(memoryStream);
using var csvWriter = new CsvWriter(streamWriter, culture: CultureInfo.InvariantCulture);
csvWriter.WriteRecords(objects);
return memoryStream;
}
private async Task DownloadFileFromStream()
{
var fileStream = GetFileStream();
using var streamRef = new DotNetStreamReference(stream: fileStream);
await JS.InvokeVoidAsync("downloadFileFromStream", FileName, streamRef);
}
}
<script>
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);
}
</script>
<MudIconButton Icon="@Icons.Material.Filled.Download" Color="Color.Secondary" Size="Size.Small" aria-label="Export to CSV" OnClick="DownloadFileFromStream">@TextDisplay</MudIconButton>
6 replies