Instance code of the file download function in asp.net

Source: Internet
Author: User

Copy codeThe Code is as follows:
// Download TransmitFile
Protected void button#click (object sender, EventArgs e)
{


Response. ContentType = "application/x-zip-compressed ";
Response. AddHeader ("Content-Disposition", "attachment?filename=z.zip ");
String filename = Server. MapPath ("DownLoad/aaa.zip ");
Response. TransmitFile (filename );
}

// Download WriteFile
Protected void Button2_Click (object sender, EventArgs e)
{


String fileName = "aaa.zip"; // file name saved by the client
String filePath = Server. MapPath ("DownLoad/aaa.zip"); // path

FileInfo fileInfo = new FileInfo (filePath );
Response. Clear ();
Response. ClearContent ();
Response. ClearHeaders ();
Response. AddHeader ("Content-Disposition", "attachment; filename =" + fileName );
Response. AddHeader ("Content-Length", fileInfo. Length. ToString ());
Response. AddHeader ("Content-Transfer-Encoding", "binary ");
Response. ContentType = "application/octet-stream ";
Response. ContentEncoding = System. Text. Encoding. GetEncoding ("gb2312 ");
Response. WriteFile (fileInfo. FullName );
Response. Flush ();
Response. End ();
}

// WriteFile multipart download
Protected void Button3_Click (object sender, EventArgs e)
{

String fileName = "aaa.zip"; // file name saved by the client
String filePath = Server. MapPath ("DownLoad/aaa.zip"); // path

System. IO. FileInfo fileInfo = new System. IO. FileInfo (filePath );

If (fileInfo. Exists = true)
{
Const long ChunkSize = 102400; // 100 K reads only 100 K each time, which can relieve the pressure on the server
Byte [] buffer = new byte [ChunkSize];

Response. Clear ();
System. IO. FileStream iStream = System. IO. File. OpenRead (filePath );
Long dataLengthToRead = iStream. Length; // obtain the total size of the downloaded file
Response. ContentType = "application/octet-stream ";
Response. AddHeader ("Content-Disposition", "attachment; filename =" + HttpUtility. UrlEncode (fileName ));
While (dataLengthToRead> 0 & Response. IsClientConnected)
{
Int lengthRead = iStream. Read (buffer, 0, Convert. ToInt32 (ChunkSize); // Read size
Response. OutputStream. Write (buffer, 0, lengthRead );
Response. Flush ();
DataLengthToRead = dataLengthToRead-lengthRead;
}
Response. Close ();
}
}

// Stream download
Protected void Button4_Click (object sender, EventArgs e)
{
String fileName = "aaa.zip"; // file name saved by the client
String filePath = Server. MapPath ("DownLoad/aaa.zip"); // path

// Download an object in the form of a streaming
FileStream fs = new FileStream (filePath, FileMode. Open );
Byte [] bytes = new byte [(int) fs. Length];
Fs. Read (bytes, 0, bytes. Length );
Fs. Close ();
Response. ContentType = "application/octet-stream ";
// Notify the browser to download the file instead of opening it
Response. AddHeader ("Content-Disposition", "attachment; filename =" + HttpUtility. UrlEncode (fileName, System. Text. Encoding. UTF8 ));
Response. BinaryWrite (bytes );
Response. Flush ();
Response. End ();

}

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.