Find a simple file to downloadCode, Write down
Original Site: http://blog.csdn.net/littlehb/archive/2007/04/20/1572440.aspx
1. Normal download:
// Download path
String Path = Server. mappath ( " Advertisement .jpg " );
// Name of the downloaded file
String Filename = " Advertisement .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:
// Download path
String Path = Server. mappath ( " Advertisement .jpg " );
// Name of the downloaded file
String Filename = " Advertisement .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 ();
}