1

There are files from 500 MB in size, along the path /mnt/test, how can I generate a link to download a file without putting this file in the site directory by type: www/name/files/.

I tried to use a file stream, but js does not have enough memory to accommodate such a size.

Tried to use the library as well https://github.com/arivera12/BlazorDownloadFile , but the problem is the same. From the library used the method

/// <summary>
///  Download a file from blazor context to the browser.
/// </summary>
/// <param name="fileName">The filename</param>
/// <param name="stream">The stream of the file</param>
/// <param name="cancellationTokenBytesRead">The cancellation token when reading bytes</param>
/// <param name="timeOutJavaScriptInterop">The timeout when executing javascript</param>
/// <param name="contentType">The file content type</param>
/// <returns></returns>
ValueTask<DownloadFileResult> DownloadFile(string fileName, Stream stream, CancellationToken cancellationTokenBytesRead, CancellationToken cancellationTokenJavaScriptInterop, string contentType = "application/octet-stream");

I found an approach through the generation of a link to a file, this approach is used by cloud storage. But I don't know how to implement, has anyone already encountered this?

Error message:

Stream was too long
Denis
  • 130
  • 12
  • Is this ASP.NET or ASP.NET Core? – DiplomacyNotWar May 24 '22 at 03:36
  • @DiplomacyNotWar I use Blazer Server, but I can adapt the code with ASP.NET Core – Denis May 24 '22 at 03:39
  • From my understanding, that [is ASP.NET Core](https://docs.microsoft.com/en-us/aspnet/core/blazor/hosting-models?view=aspnetcore-6.0#blazor-server), isn't it? Anyway, you might want to look at [`FileStreamResult`](https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.filestreamresult?view=aspnetcore-6.0) if you want to return a link to a file (this would be the downloader method). – DiplomacyNotWar May 24 '22 at 03:42
  • @DiplomacyNotWar Will I have to make an Api that will return FileStreamResult? And it turns out that we simply redirect the user there at the click of a button? – Denis May 24 '22 at 03:52
  • 1
    I want to say that I'm not really familiar with Blazor, so there may be a proper Blazor way of doing this. But, yeah, you would simply create an ASP.NET Core controller (you'd need to set up controllers in your services and app configuration) that takes some information about the file you need to return, then you can either use `FileResult` (if you want to use the local file) or `FileStreamResult` (if you want to use a stream) to return that file from the controller action. Obvious gotcha: don't let the user just specify any file. Maybe sign or encrypt the URL, or something. – DiplomacyNotWar May 24 '22 at 04:00
  • Blazor server streams html updates over a signalR connection whereupon a client side js device patches them into the dom, but that doesn't mean you have to send file downloads over it too, and have the js collect it then fake it as a download. The browser is still capable of visiting a URL using normal HTTP and streaming what it finds there. Generate the file in response to a regular request and stream it down the socket so you aren't storing it Server side – Caius Jard May 24 '22 at 05:35
  • Essentially, a duplicate of https://stackoverflow.com/questions/42460198/return-file-in-asp-net-core-web-api – Caius Jard May 24 '22 at 05:39

0 Answers0