1. // download TransmitFile
Protected void button#click (object sender, EventArgs e)
{
/*
Microsoft provides a new TransmitFile method for the Response object to solve the problem of using Response. BinaryWrite
When a file with more than Mbit/s is downloaded, The aspnet_wp.exe process cannot be recycled.
The Code is as follows:
*/
Response. ContentType = "application/x-zip-compressed ";
Response. AddHeader ("Content-Disposition", "attachment?filename=z.zip ");
String filename = Server. MapPath ("DownLoad/z.zip ");
Response. TransmitFile (filename );
}
Ii. // download WriteFile
Protected void Button2_Click (object sender, EventArgs e)
{
/*
Using System. IO;
*/
String fileName = "asd.txt"; // file name saved by the client
String filePath = Server. MapPath ("DownLoad/aaa.txt"); // 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 ();
}
Iii. // multipart download of WriteFile
Protected void Button3_Click (object sender, EventArgs e)
{
String fileName = "aaa.txt"; // file name saved by the client
String filePath = Server. MapPath ("DownLoad/aaa.txt"); // 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 ();
}
}
Iv. // stream download
Protected void Button4_Click (object sender, EventArgs e)
{
String fileName = "aaa.txt"; // file name saved by the client
String filePath = Server. MapPath ("DownLoad/aaa.txt"); // 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 ();
}
//----------------------------------------------------------
Public void DownloadFile (System. Web. UI. Page WebForm, String FileNameWhenUserDownload, String FileBody)
{
WebForm. Response. ClearHeaders ();
WebForm. Response. Clear ();
WebForm. Response. Expires = 0;
WebForm. Response. Buffer = true;
WebForm. Response. AddHeader ("Accept-Language", "zh-tw ");
// 'File name
WebForm. response. addHeader ("content-disposition", "attachment; filename = '" + System. web. httpUtility. urlEncode (FileNameWhenUserDownload, System. text. encoding. UTF8) + "'");
WebForm. Response. ContentType = "Application/octet-stream ";
// 'File content
WebForm. Response. Write (FileBody );//-----------
WebForm. Response. End ();
}
// The above code downloads a dynamically generated text file. If the file already exists in the server-side object path, you can use the following function:
Public void DownloadFileByFilePath (System. Web. UI. Page WebForm, String FileNameWhenUserDownload, String FilePath)
{
WebForm. Response. ClearHeaders ();
WebForm. Response. Clear ();
WebForm. Response. Expires = 0;
WebForm. Response. Buffer = true;
WebForm. Response. AddHeader ("Accept-Language", "zh-tw ");
// File name
WebForm. response. addHeader ("content-disposition", "attachment; filename = '" + System. web. httpUtility. urlEncode (FileNameWhenUserDownload, System. text. encoding. UTF8) + "'");
WebForm. Response. ContentType = "Application/octet-stream ";
// File Content
WebForm. Response. Write (System. IO. File. Rea} dAllBytes (FilePath ));//---------
WebForm. Response. End ();
}