Report on progress bar of download file

Dani_S 4,461 Reputation points
2025-03-31T16:59:49.5233333+00:00

Hi,

I have progress bar control, i got from API the content stream response.

How i can report on progress of download process of file in the UI page?

Thanks,

try
{
 using (var fileStream = File.Create(zipPathForFolderOrPathForFile))
             contentStream.CopyTo(fileStream);
}
catch(Exception ex)
{
}
finally
{
contentStream.Dispose();
}
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
4,070 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 74,531 Reputation points
    2025-03-31T21:13:34.77+00:00

    instead of using .CopyTo(), you write your own copy with a progress callback:

    public void StreamCopy(Stream input, Stream output, Action<int> progressCallback)
    {
        int bytesRead = 0;
        byte[] buffer = new byte[1024 * 8]; // 8k buffer 
        while ((var read = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            output.Write (buffer, 0, read);
            bytesRead += read;
            progressCallback(read);
        }
    }
    

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

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.