Several common download methods for asp.net are as follows. You can add
<Iframe id = "iDownload" style = "display: none;"> </iframe>
Then, use the jquery statement to specify the download link.
$ ("# IDownload"). attr ("src", "download. ashx? Filename = "+ encodeURI ($ (this). children (" span "). first (). text ()));
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/aaa.zip ");
Response. TransmitFile (filename );
}
// Download WriteFile
Protected void Button2_Click (object sender, EventArgs e)
{
/*
Using System. IO;
*/
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 & gt; 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 ();
}
From a blog