C # File Download Method

Source: Internet
Author: User

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 ();}}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.