Ask Your Question

In an ASP.NET Core 7 Web API controller, how can I implement a server side progress function while uploading a file to the client using C#?

asked 2022-03-02 11:00:00 +0000

woof gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2021-09-29 01:00:00 +0000

lakamha gravatar image

You can implement a server side progress function while uploading a file to the client in ASP.NET Core 7 Web API controller using the following steps:

  1. Add the following NuGet packages to your project:
  • Microsoft.AspNetCore.Http.Features
  • Microsoft.AspNetCore.WebUtilities
  1. In your controller action that handles the file upload, declare an instance of IFormFile and cast the incoming Request.Form.Files parameter to it.

public async Task<iactionresult> UploadFile(IFormFile file)

  1. Get the length of the uploaded file and use it to calculate the percentage of the file that has been uploaded.

var totalBytes = file.Length; var uploadedBytes = 0L;


if (chunkedFile.Length > 0) { await fileStream.WriteAsync(chunkedFile, cancellationToken); uploadedBytes += chunkedFile.Length;

// Calculate progress percentage
var progress = Convert.ToInt32((uploadedBytes * 100) / totalBytes);


  1. Return the progress value to the client.

return Ok(progress);

  1. On the client side, use AJAX or WebSockets to make periodic requests to the server to get the progress value and update the user interface accordingly.
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer

Question Tools


Asked: 2022-03-02 11:00:00 +0000

Seen: 14 times

Last updated: Sep 29 '21