Asp.net large file download

Source: Internet
Author: User

A retriable solution that uses binary stream downloads is like a http://www.abc.com/down.apsx? For a connection such as fileid = 911199, the permission is determined on the down page, for example, whether the user has logged on, and whether the number of current user points meets the number of download points of the corresponding file of 911199.

Binary Download instance code, supporting extra-large file downloads

String downFilePath = @ "D: \ openSUSE-10.2-GM-DVD-i386.iso"; // test with 3.8 GB, OK // Server. MapPath ("~ /Files/somefile. iso ");
System. IO. FileInfo downFileInfo = new System. IO. FileInfo (downFilePath );

If (! DownFileInfo. Exists) throw new Exception ("the file does not exist. ");
Const int CHUNK_SIZE = 10000; // specify the block size
Byte [] buffer = new byte [CHUNK_SIZE];

Response. Clear ();
// Fails to down the big file with both the following methods
// Error: System. ArgumentOutOfRangeException: The size parameter must be between zero and the maximum Int32 value.
// Response. WriteFile (downFilePath );
// OR
// Response. TransmitFile (downFilePath); // ASP. NET 2.0 supported
//
Using (System. IO. FileStream iStream = System. IO. File. OpenRead (downFilePath )){
Long dataLengthToRead = iStream. Length;
Response. ContentType = "application/octet-stream ";
Response. AddHeader ("Content-Disposition ",
"Attachment; filename =" + Server. UrlPathEncode (downFileInfo. Name); // encode the file Name
While (dataLengthToRead> 0 & Response. IsClientConnected ){
Int lengthRead = iStream. Read (buffer, 0, CHUNK_SIZE );
Response. OutputStream. Write (buffer, 0, lengthRead );
Response. Flush ();
DataLengthToRead = dataLengthToRead-lengthRead;
}
}
Response. Close ();

 


Author Zheng wenliang

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.