Issue in Blazor: File Upload with Zero Byte Size

Yusuf 791 Reputation points
2024-08-01T20:30:55.56+00:00

Hello,

I'm facing an issue with my Blazor application when attempting to upload a file. Sometimes the file uploads correctly, but other times the file is uploaded with a size of zero bytes.

The issue does not always occur, making it difficult to pinpoint the root cause.

Here is a simplified version of the code I am using:

 <div class="mb-3">
     <InputFile OnChange="HandleFileSelected" />
 </div>

private async Task HandleFileSelected(InputFileChangeEventArgs e)

{

    var uploadPath = Path.Combine(env.WebRootPath, "Upload");

    if (!Directory.Exists(uploadPath))

    {

        Directory.CreateDirectory(uploadPath);

    }

    var file = e.File;

    if (file != null && file.Size > 0)

    {

        var filePath = Path.Combine(uploadPath, file.Name);

        using (var stream = new FileStream(filePath, FileMode.Create))

        {

            await file.OpenReadStream(maxAllowedSize: 200 * 1024 * 1024).CopyToAsync(stream);

        }

    }

}

Are there any known reasons why this issue might be happening in Blazor? Could there be a problem with the project configuration or environment? Any help would be greatly appreciated.

Thank you in advance!


ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,573 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,584 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 65,576 Reputation points
    2024-08-02T21:27:28.5933333+00:00

    While blazor signal/r datagram tracing is difficult, you could use the browser tools to see any messages are appearing.


  2. AgaveJoe 28,371 Reputation points
    2024-08-03T09:42:40.78+00:00

    It seems that large files are uploaded to Plesk with a size of 0 bytes until the upload is complete. I was clicking a button that interacts with the uploaded file before the upload is finished, Is there a way to check if the upload is complete or to wait until the upload is finished?

    The official documentation has code examples for canceling a file upload and implementing file upload progress.

    https://zcusa.951200.xyz/en-us/aspnet/core/blazor/file-uploads?view=aspnetcore-8.0#cancel-a-file-upload

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.