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