C # winform File Download

Source: Internet
Author: User
///   <Summary>          ///  Download file (display progress)  ///   </Summary>          ///   <Param name = "url"> </param>          ///   <Param name = "FILENAME"> </param>          ///   <Param name = "prog"> </param>          Public   Void Downloadfile (String URL, String  Filename, system. Windows. Forms. progressbar prog ){  Try  {System. net. httpwebrequest myrq = (System. net. httpwebrequest) system. net. httpwebrequest. Create (URL); system. net. httpwebresponse myrp = (System. net. httpwebresponse) myrq. getresponse ();  Long Totalbytes = Myrp. contentlength;  If (Prog! =Null  ) {Prog. Maximum = ( Int  ) Totalbytes;} system. Io. Stream St = Myrp. getresponsestream (); system. Io. stream so = New  System. Io. filestream (filename, system. Io. filemode. Create );  Long Totaldownloadedbyte = 0  ;  Byte [] By =New   Byte [ 1024  ];  Int Osize = ST. Read (, 0 ,( Int  ) By. Length );  While (Osize> 0  ) {Totaldownloadedbyte = Osize + Totaldownloadedbyte; system. Windows. Forms. application. doevents (); so. Write (, 0  , Osize );  If (Prog! = Null  ) {Prog. Value = ( Int  ) Totaldownloadedbyte;} osize = ST. Read (, 0 ,( Int  ) By. Length) ;}so. Close (); ST. Close ();}  Catch (System. Exception ){  Throw  ;}}  ///   <Summary>          ///  Download files  ///   </Summary>          ///   <Param name = "url"> </param>          ///   <Param name = "FILENAME"> </param>          Public   Void Downloadfile ( String URL, String  Filename) {downloadfile (URL, filename,  Null  );} 

 

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.