In Blazor WASM I have this code:
protected async Task Download()
{
base64Data = await Http.GetByteArrayAsync(url);
await jsRunTime.InvokeVoidAsync("downloadFile", "application/pdf", base64Data, "test.pdf");
}
And in the JavaScript file I have this:
function downloadFile(contentType, base64Data, fileName) {
const linkSource = `data:${contentType};base64,${base64Data}`;
const downloadLink = document.createElement("a");
downloadLink.href = linkSource;
downloadLink.download = fileName;
downloadLink.click();
}
If I try to get a really small pdf it works, but if the base64 string is too large (a file with over 400kB), I have a network failure when downloading the file. A strange thing is on Edge network tab, at Fetch/XHR, don't matter the file I try to download, the size is always 576kB.
Any idea of what to do? Thanks!
I finally made it work.
I had to use GetStringAsync and Trim the server response because the base64 string was surrounded by quotation marks.
protected async Task Download()
{
base64Data = await Http.GetStringAsync(url);
base64Data = base64Data.Trim('"');
await jsRunTime.InvokeVoidAsync("downloadFile", "application/pdf", base64Data, "test.pdf");
}
Thanks guys!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With