Image upload stuck with blazor server and BlazorInputFile

2.1k views Asked by At

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);
        }
    }
}
0

There are 0 answers