Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

blazor server upload file stream to api cdn

$
0
0

In Blazor.Server you need to upload files over 400 MB to CDN.

Code that works fine with InteractiveWebAssembly

private async Task OnInputFileChange(InputFileChangeEventArgs e){const long CHUNKSIZE = 1024 * 400; // subjectivevar file = e.File;long uploadedBytes = 0;long totalBytes = file.Size;long percent = 0;int fragment = 0;long chunkSize;using (var inStream = file.OpenReadStream(long.MaxValue)){    blockView = false;    if (!string.IsNullOrEmpty(model.Url))    {        model.Url = null;    }    _uploading = true;    var contentType = Path.GetExtension(file.Name);                while (_uploading)    {        chunkSize = CHUNKSIZE;        if (uploadedBytes + CHUNKSIZE > totalBytes)        {// remainder            chunkSize = totalBytes - uploadedBytes;        }        var chunk = new byte[chunkSize];        await inStream.ReadAsync(chunk, 0, chunk.Length);        // upload this fragment        var fileContent = new StreamContent(new MemoryStream(chunk));        var request = new HttpRequestMessage(HttpMethod.Put, $"https://{cdn_url}/{model.Files.Name}/{(fragment++).ToString("D8")}");        request.Headers.Add("X-Auth-Token", model.Files.Token);        request.Content = fileContent;        // put        var response = await Http.SendAsync(request);        // Update our progress data and UI        uploadedBytes += chunkSize;        percent = uploadedBytes * 100 / totalBytes;        echo = $"Dowloand {percent}%  {(uploadedBytes / 1e+6).ToString("F2")} mb in {(totalBytes / 1e+6).ToString("F2")} mb";        if (percent >= 100)        {// upload complete            _uploading = false;        }        Console.WriteLine(fragment +" " + fileContent.Headers.ContentLength);        await InvokeAsync(StateHasChanged);    }    var requestItog = new HttpRequestMessage(HttpMethod.Put, $"https://{cdn_url}/{model.Files.Name + contentType}");    requestItog.Headers.Add("X-Auth-Token", model.Files.Token);    requestItog.Headers.Add("X-Object-Manifest", $"{Container_Name}/{model.Files.Name}/");    // put    var responseItog = await Http.SendAsync(requestItog);    model.Url = model.Files.Name + contentType;    AddName.InvokeAsync(model.Files.Name + contentType);}}

added to the program.cs

builder.Services.AddServerSideBlazor()                .AddHubOptions(o => o.MaximumReceiveMessageSize = 100000000);

checking the 1.37 MB image showed that they arrived intact in size but broken.

enter image description here

How to avoid broken bytes.Tried to reduce the size of CHUNKSIZE to 32 kb, despite the longer loading the file is still broken. Although much smaller.


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>