0

I am trying to download a file from an url using WebClient and multi thread. I am trying to get ProgressPercentage while downloading file and I am trying to do that with DownloadProgressChanged but when I try to print percentage to with debug.WriteLine everything is okey. But when I want to change my textbox.text its not working. I want to update my textbox while downloading.

With this one I am calling a function. wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressChangedEventHandler);

This is the content of function:

 public void DownloadProgressChangedEventHandler(object sender, DownloadProgressChangedEventArgs e)
    {
        //Debug.WriteLine works normal.
        Debug.WriteLine(e.ProgressPercentage);
        TextBox1.Text = "Progress:" +e.ProgressPercentage.ToString();
        progressBarDownload3.Style.Add("width",e.ProgressPercentage.ToString()+"%");
    }
Orhan Özkerçin
  • 342
  • 1
  • 4
  • 14

1 Answers1

1

Technically, While the solution above could be useful if you are working on a desktop or mobile application but if you are using ASP.NET Server-Side Component and your server is downloading something for you while the page is being posted back, then there is no way the front end can be updated before the download is completed and response is sent back to the requesting client. This is the whole sort of point, every request that goes to the server will only return once the response is completed/interrupted, it cannot come back just to report the progress and then go back to the point where it was running.

If you want to display the progress of the file that is being downloaded then you should go for a client side (JavaScript/jQuery) based solutions.

If you strictly want to download the file with C# code then try checking out for SignalR library, it could be helpful for you in reporting the changes in the backend to the frontend but at the same time, it would be complicated to manage just to report download progress.

Alternatively, you can throw the browser to the download url and it would download the file automatically for the client and the browser's default download progress bar would be visible to the clients downloading the file.

No matter what, you cannot download files from the server and report each byte that gets downloaded to the front end using C# and ASP.NET WebForms/MVC. Unless you include jquery/javascript in the action.

Jamshaid K.
  • 3,555
  • 1
  • 27
  • 42
  • Thank you for helping, what about using Js/Jquery, how can access this data using Js? – Orhan Özkerçin May 05 '20 at 11:46
  • @OrhanÖzkerçin, If you are planning to use jQuery/JavaScript, then you will have to find out the libraries that allow downloading feature, quick search leads me to this SO answer: https://stackoverflow.com/a/39599878/4308286 – Jamshaid K. May 05 '20 at 15:33