// Download transmitfile
// Download transmitfile
Protected void button1_click1 (Object sender, eventargs E)
{
String strfilename = "three-part idle device management system operation manual iems.ppt ";
Response. contenttype = "application/X-zip-compressed ";
// Response. contentencoding = system. Text. encoding. getencoding ("gb2312 ");
String filename = BLL. config. part_em_upload_doc + strfilename;
// BLL. config. part_em_upload_doc is the path ("D:/emuploaddoc /")
Response. addheader ("content-disposition", "attachment; filename =" + server. urlpathencode (strfilename ));
// Server. urlpathencode () solves the file name garbled problem.
Response. transmitfile (filename );
} // Download writefile
Protected void button2_click (Object sender, eventargs E)
{
String filename = "asd.txt"; // file name saved by the client
String filepath = server. mappath ("download/aaa.txt"); // path
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.txt"; // file name saved by the client
String filepath = server. mappath ("download/aaa.txt"); // path
System. Io. 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 ();
}
}
// 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 ();
}