1. Normal download:
// File Download path
String path = Server. MapPath ("advertising .jpg ");
// Name of the downloaded object
String filename = "advertising .jpg ";
System. IO. FileInfo toDownload = new System. IO. FileInfo (path );
Response. Clear ();
If (System. IO. Path. GetExtension (filename) = ". jpg ")
...{
Response. AddHeader ("Content-Disposition", "attachment; filename = NEW _" + HttpUtility. UrlEncode (toDownload. Name ));
Response. ContentType = "application/x-zip-compressed ";
Response. TransmitFile (path );
Response. End ();
}
2. Split large files into small pieces for download:
// File Download path
String path = Server. MapPath ("advertising .jpg ");
// Name of the downloaded object
String filename = "advertising .jpg ";
System. IO. FileInfo toDownload = new System. IO. FileInfo (path );
If (toDownload. Exists = true)
{
Const long ChunkSize = 10000;
Byte [] buffer = new byte [ChunkSize];
Response. Clear ();
System. IO. FileStream iStream = System. IO. File. OpenRead (path );
Long dataLengthToRead = iStream. Length;
Response. ContentType = "application/octet-stream ";
Response. AddHeader ("Content-Disposition", "attachment; filename = new _" + HttpUtility. UrlEncode (toDownload. Name ));
While (dataLengthToRead> 0 & Response. IsClientConnected)
{
Int lengthRead = iStream. Read (buffer, 0, Convert. ToInt32 (ChunkSize ));
Response. OutputStream. Write (buffer, 0, lengthRead );
Response. Flush ();
DataLengthToRead = dataLengthToRead-lengthRead;
}
Response. Close ();
}
From Ai Zhi Chen