C # download with progress bar code (normal progress bar)

Source: Internet
Author: User
Tags file url url example
<span style= "FONT-SIZE:14PX;" > </span><pre name= "code" class= "CSharp" ><span style= "FONT-SIZE:14PX;" >//<summary>//download with progress bar code (normal progress bar)///</summary>//<param name= "UR L "> URL </param>//<param name=" filename "> after download file name </param>//<param name=" Prog "& GT; Report progress processing (first parameter: Total size, second parameter: current progress) </param>//<returns>true/false Download Success </returns> Publi            C static BOOL DownLoadFile (string URL, String Filename, Action<int, int> updateprogress = null) {            Stream st = null;             Stream so = null;             System.Net.HttpWebRequest MYRQ =null;            System.Net.HttpWebResponse MYRP = null;            BOOL flag = FALSE;                      try {Myrq = (System.Net.HttpWebRequest) System.Net.HttpWebRequest.Create (URL);//Get a Web request from the URL address MYRP = (System.Net.HttpWebResponse) myrq.getresponse ();Get Web response from Web request long totalbytes = Myrp. ContentLength; Total bytes received from Web Response//Update progress if (updateprogress! = null) {update Progress (int) totalbytes,0);//The maximum value of the progress bar is obtained from the total number of bytes} st = Myrp. GetResponseStream (); Create stream from Web request (read) so = new System.IO.FileStream (Filename, System.IO.FileMode.Create); Create file stream (write) long totaldownloadedbyte = 0;                Download file size byte[] by = new byte[1024]; int osize = St. Read (by, 0, (int.) by. Length); Read stream while (Osize > 0) {totaldownloadedbyte = osize + totaldownl Oadedbyte;                    Update file size application.doevents (); So. Write (by, 0, osize);                        Write stream//update progress if (updateprogress! = null) { UpdateProgress ((int) totalbytes, (int) TotaldownloadedbyTE);//Update progress bar} osize = St. Read (by, 0, (int.) by. Length);                    Read Stream}//Update progress if (updateprogress! = null) {                UpdateProgress ((int) totalbytes, (int) totalbytes);            } flag= true;                } catch (Exception) {flag = false;                Throw            return false; } finally {if (MYRQ! = null) {Myrq.abort ();//Destroy Off Connect} if (MYRP! = null) {MYRP. Close ();//Destroy Close response} if (so! = null) {so. Close (); Close Stream} if (st! = null) {St. Close ();        Close Stream}} return flag; }</span>



Call method One:


<span style= "FONT-SIZE:14PX;" > If (fileupdownload.downloadfile ("url of download file", "filename", New Action<int, int> (        int Maximum, int Value)        = >        {            //update progress bar            progressbar1.maximum = Maximum;            progressBar1.Value = Value;})))        {//the processing}</span> after downloading the file


Action notation for non-pass arguments:

(form) Form). BeginInvoke (new Action () =            {                //process            })


Call Mode two:



<span style= "FONT-SIZE:14PX;" > </span><pre name= "code" class= "CSharp" ><span style= "FONT-SIZE:14PX;" > If (fileupdownload.downloadfile ("Download file url", "filename", UpdateProgressBar)) {//download file after processing}////update progress bar private void UpdateProgressBar (int Maximum, int Value) {     progressbar1.maximum = Maximum;     progressBar1.Value = Value;} </span>




Small bet: The PROGRESSBAR1 in the calling code is Microsoft's Progress bar control


URL Example: http://www.php.cn/


If you want to charge Windows Server to download files, you need to configure in IIS, the directory is accessible, the following configuration steps are as follows:

1. Locate the directory where you want to download the file:


2. Find "Directory Browsing" on the right:


3, in the "Directory Browsing" right click: Open the function


4, you can see, enable:


The above is the C # download with progress bar code (ordinary progress bar) content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.