mwrochna
mwrochna

Reputation: 156

ASP .net 6 download file by httpclient - problem with stream

I'm creating blazor server app. I use external file storage with RestAPI. I want to create download button to get file from storage. This may seem easy, but not necessarily.

From file storage I download HttpContent like that

        var request = new HttpRequestMessage(HttpMethod.Get, _url);
        request.Headers.Add("auth-token", token);
        request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/octet-stream"));
        HttpResponseMessage response = await _Http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
        response.EnsureSuccessStatusCode();
        var content = response.Content;

next I act like this tutorial https://learn.microsoft.com/en-us/aspnet/core/blazor/file-downloads?view=aspnetcore-6.0

        var fileStream = content.ReadAsStream();
        using (var streamRef = new DotNetStreamReference(fileStream))
        {
            await JS.InvokeVoidAsync("downloadFileFromStream", "file.txt", streamRef);
        }  

For small files everything work great. But if I try to download large file (100mb), algoritm firstable download file to memory(RAM) of server and later save on local disk of client.

In ideal world I dream that when I click button download, file from external storage will download after delay (with progressbar) like physical file (no stream buffer) form http server e.g. https://www.example.com/file.txt. Of course by my BlazorServer Application with authorization and authentication, and whole neccesery services.

Upvotes: 3

Views: 2482

Answers (1)

mwrochna
mwrochna

Reputation: 156

I have solution.

  1. Create Service to service File Storage API
  2. Create controller to avoid cros-origin error
  3. Use microsoft tutorial to create download button https://learn.microsoft.com/en-us/aspnet/core/blazor/file-downloads?view=aspnetcore-6.0

Upvotes: 2

Related Questions