Record the method for downloading an object.
Http://topic.csdn.net/u/20120822/10/d8115bb7-2f2a-4c2f-b0c1-aab68bcb1e3e.html
Using system; using system. data; using system. configuration; using system. web; using system. web. security; using system. web. ui; using system. web. UI. webcontrols; using system. web. UI. webcontrols. webparts; using system. web. UI. htmlcontrols; using system. io; public partial class _ default: system. web. UI. page {protected void page_load (Object sender, eventargs e) {}// transmitfile implement download protected void button#click (Object sender, eventargs e) {response. contenttype = "application/X-zip-compressed"; response. addheader ("content-disposition", "attachment?filename=z.zip"); string filename = server. mappath ("download/z.zip"); response. transmitfile (filename);} // writefile implements the download of protected void button2_click (Object sender, eventargs e) {string filename = "asd.txt"; // 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"; // 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 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 mode to download protected void button4_click (Object sender, eventargs e) {string filename = "aaa.txt"; // the file name saved by the client string filepath = server. mappath ("download/aaa.txt"); // 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 ();}}