C # http File download public class (support for breakpoint continuation)

Source: Internet
Author: User

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.IO;6 usingSystem.Net;7 8 namespaceXcdownloadfile9 {Ten      Public classDownLoadFile One     { A         /// -         ///Download File Method -         /// the         ///file save path and filename -         ///returns the server file name -         /// -          Public BOOLDeownloadfile (stringstrFileName,stringfile) +         { -             BOOLFlag =false; +             //Open the last downloaded file A             LongSposition =0; at             //instantiating a Stream object - FileStream fstream; -             //determine if the folder you want to download exists -             if(File.exists (strFileName)) -             { -                 //Open the file you want to download inFStream =File.openwrite (strFileName); -                 //get the length that has been downloaded toSposition =fstream.length; + Fstream.seek (sposition, seekorigin.current); -             } the             Else *             { $                 //file is not saved create a filePanax NotoginsengFStream =NewFileStream (strFileName, filemode.create); -Sposition =0; the             } +             Try A             { the                 //Open Network Connection +HttpWebRequest myrequest = (HttpWebRequest) httpwebrequest.create ("http://www.newxing.com/download/"+file); -                 if(Sposition >0) $Myrequest.addrange ((int) sposition);//Set Range value $                 //request to the server, get the response data stream of the server -Stream MyStream =Myrequest.getresponse (). GetResponseStream (); -                 //define one byte of data the                 byte[] Btcontent =New byte[ +]; -                 intIntSize =0;WuyiIntSize = Mystream.read (Btcontent,0, +); the                  while(IntSize >0) -                 { WuFstream.write (Btcontent,0, intSize); -IntSize = Mystream.read (Btcontent,0, +); About                 } $                 //Close the stream - fstream.close (); - mystream.close (); -Flag =true;//return true Download succeeded A             } +             Catch(Exception) the             { - fstream.close (); $Flag =false;//return false Download failed the             } the             returnFlag; the         } the     } -}
View Code

Thank you: http://www.cnblogs.com/skyay/p/3880221.html

C # http File download public class (support for breakpoint continuation)

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.