Click the download button to download the file to local download stream

Source: Internet
Author: User

 protected void Down_load (object sender, EventArgs e) {//create a stream for the file stream stream = null;        This controls how many bytes to read at a time and send to the client int bytestoread = 10000;        Buffer to read bytes in chunk size specified above byte[] buffer = new Byte[bytestoread];        String url = "Http://stv1.222.net/mv/dkp10608.mp4";        String fileName = "11.mp4"; Try {//create a WebRequest to get the file HttpWebRequest Filereq = (HttpWebRequest) webreque St.            Create (URL);            HttpWebResponse Fileresp = (HttpWebResponse) filereq.getresponse ();            if (Filereq.contentlength > 0) fileresp.contentlength = filereq.contentlength;            stream = Fileresp.getresponsestream ();            var resp = HttpContext.Current.Response; Resp.            ContentType = "Application/octet-stream"; Indicate the type of data being sent resp. AddheadeR ("Content-disposition", "attachment;filename=\" "+ filename +" \ ""); Resp.            AddHeader ("Content-length", fileResp.ContentLength.ToString ());            int length; Do {//verify-client is connected if (resp. isclientconnected) {//read data into the buffer length = stream.                    Read (buffer, 0, bytestoread); and write it out to the response ' s output stream resp.                    Outputstream.write (buffer, 0, length); Flush the Data resp.                    Flush ();                Clear the data buffer = new Byte[bytestoread];                    } else {//cancel The download if client has disconnected                length =-1; }} while (length > 0);          Repeat until no data is read} catch (Exception) {  Throw                } finally {if (stream! = null) {//close the input stream Stream.            Close (); }        }}

  

Click the download button to download the file to local download stream

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.