Protected void Button1_Click (object sender, EventArgs e) {/* Microsoft provides a new method for the Response object, TransmitFile, to solve the problem of failure to download successfully when the Response. BinaryWrite handler process is 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 );}
// WriteFile implements the download of 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 read only 100 K each time, this 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; // get 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"; // the file name saved by the client string filePath = Server. mapPath ("DownLoad/aaa.zip"); // path
// Download the file 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 Response. addHeader ("Content-Disposition", "attachment; filename =" + HttpUtility. urlEncode (fileName, System. text. encoding. UTF8); Response. binaryWrite (bytes); Response. flush (); Response. end ();
}