1PublicStaticVoidDownload (page,StringFilename) 2{ 3Try 4{ 5System. Io. filestream R=NewSystem. Io. filestream (filename, system. Io. filemode. Open ); 6//Set Basic Information 7Page. response. Buffer=False; 8Page. response. addheader ("Connection","Keep-alive"); 9Page. response. contenttype="Application/octet-stream"; 10Page. response. addheader ("Content-Disposition","Attachment; filename ="+System. Io. Path. getfilename (filename )); 11Page. response. addheader ("Content-Length", R. length. tostring ()); 12While(True) 13{ 14//Buffer space opening 15Byte[] Buffer=NewByte[1024]; 16//Read File data 17IntLeng=R. Read (buffer,0,1024); 18If(Leng=0)//End at the end of the file 19Break; 20If(Leng=1024)//The read file data length is equal to the buffer length, and the buffer data is directly written 21Page. response. binarywrite (buffer ); 22Else 23{ 24//Reading file data is smaller than the buffer, and the buffer size is redefined. It is only used to read the last data block of the file. 25Byte[] B=NewByte[Leng]; 26For(IntI=0; I<Leng; I++) 27B [I]=Buffer [I]; 28Page. response. binarywrite (B ); 29Break; 30} 31} 32R. Close ();//Close File Download 33Httpcontext. Current. applicationinstance. completerequest ();//End File Download 34Page. response. Flush (); 35Page. response. Close (); 36} 37Catch(System. Exception ex) 38{ 39ThrowEx; 40} 41}
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