Search code examples
iisserverblazorwindows-server-2019

Blazor File Upload Not Working post IIS Deployment


I use this code to upload files to the blazor server project,

 private async Task uploadFile(InputFileChangeEventArgs e, ClaimDetailDTO claimDetail)//Uploads file to server
{
    var file = e.File;
    if (file.Size > maxFileSize)
    {
        FilePopupVisible = true;
    }
    else
    {
        isLoading = true;
        loadedFiles.Clear();
        progressPercent = 0;
        try
        {
            trustedFileName = claimDetail.ClaimDetailId.ToString() + file.Name.Substring(file.Name.IndexOf('.'));
            string rootPath = @"C:\inetpub\wwwroot\Claims Blazor\Claim Attachments\";
            var path = Path.Combine(rootPath, trustedFileName);

            await using FileStream writeStream = new(path, FileMode.Create);
            using var readStream = file.OpenReadStream(maxFileSize);
            var bytesRead = 0;
            var totalRead = 0;
            var buffer = new byte[1024 * 10];

            while ((bytesRead = await readStream.ReadAsync(buffer)) != 0)
            {
                totalRead += bytesRead;

                await writeStream.WriteAsync(buffer, 0, bytesRead);

                progressPercent = Decimal.Divide(totalRead, file.Size);

                StateHasChanged();
            }
            loadedFiles.Add(file);
            claimDetail.HasAttachement = true;
        }
        catch (Exception ex)
        {
            _logger.LogError("File: {Filename} Error: {Error}",
                file.Name, ex.Message);
        }
        isLoading = false;
    }
}

Post deployment to IIS server the upload doesn't appear to work. There are no error messages in the logs so I don't think its tripping the exception. Anyone know where the issue could be.


Solution

  • According to your description, if there is no problem with your code, I guess the following reasons may cause the problem, you can check them.

    The IIS user account may not have permission to upload files to the server. You need to check the permissions of the directory where the uploaded file is located and make sure the IIS user account has write permissions.

    Also, if you try to upload a file that is too large, the upload may fail because IIS has a default maximum file size limit of 30 MB. Of course, you can increase the file size limit by modifying the maxAllowedContentLength setting in your web.config file.

    It is also possible that your server's firewall is blocking file upload requests. You need to check your firewall settings or network restrictions.