4

I have a problem while uploading images to an app with blazor server.

I am currently using BlazorInputFile because I am using .Net Core 3.1

When I try to upload a series of images (100 images of about 500 kb size) after a while the upload is interrupted and the image gets corrupted. When the image in the upload destination folder is interrupted, it is always like this:

When the image in the upload destination folder is interrupted, it is always like this

So this is my code for upload images:

<InputFile id="fileInput112" OnChange="SelectFiles" hidden multiple accept=".jpg, .jpeg, .png" />

@code{

    private async void SelectFiles(IFileListEntry[] files)
    {
        var folderProductImage = "images/products/";
        
        foreach (var item in files)
        {
            await Task.Run(() => UploadProductImage(item, folderProductImage));
        }
    }
    
    private async Task UploadProductImage(IFileListEntry item, string destDirName)
    {
        string fileUploadPath = Path.Combine(destDirName, item.Name);
        
        using (FileStream writer = new FileStream(fileUploadPath, FileMode.Create, FileAccess.ReadWrite))
        {
            await item.Data.CopyToAsync(writer);
        }
    }
}
Lorenzo Belfanti
  • 1,205
  • 3
  • 25
  • 51
  • Do you see anything interesting in the browser console output? – Kirk Woll Jun 04 '21 at 21:18
  • 2
    OpenReadStream enforces a maximum size in bytes of its Stream. Reading one file or multiple files larger than 512,000 bytes (500 KB) results in an exception. This limit prevents developers from accidentally reading large files in to memory. The maxAllowedSize parameter of OpenReadStream can be used to specify a larger size if required. - it's per MS documentation here. https://learn.microsoft.com/en-us/aspnet/core/blazor/file-uploads?view=aspnetcore-5.0&pivots=server . Do you want to post your server's logs so we can look into this? – NewRK Jun 04 '21 at 23:45
  • 1
    @NewRK I found this problem reported in the github. I tried to do as the last answer says and now it seems to work. https://github.com/SteveSandersonMS/BlazorInputFile/issues/17 `` – Lorenzo Belfanti Jun 08 '21 at 14:18
  • Did you ever resolve this? I have no errors and tried MaxBufferSize with no luck. – GisMofx Mar 19 '22 at 01:39
  • @GisMofx As written in the comment before I solved by increasing the `MaxBufferSize`. – Lorenzo Belfanti Mar 22 '22 at 11:28
  • Thanks. That didn't work for me. Turns out something changed between Net5 and Net6. I now have to resize on the server until I can come up with a JS solution – GisMofx Mar 26 '22 at 00:57
  • @GisMofx in this case i used `BlazorInputFile` because my project was .net core 3.1. If you use .net 5 or beyond you can use the native `InputFile`. – Lorenzo Belfanti Mar 29 '22 at 13:35

0 Answers0