C # methods for downloading files

Source: Internet
Author: User
Public void METH1 () {/* Microsoft provides a new transmitfile method for the response object to solve the problem that the aspnet_wp.exe process cannot be successfully downloaded when the response. binarywrite file is downloaded over Mbit/s. Code As follows: */response. contenttype = "application/X-zip-compressed"; response. addheader ("content-disposition", "attachment?filename=z.zip"); string filename = server. mappath ("download/z.zip"); response. transmitfile (filename);} public void meth2 () {/* using system. io; */string filename = "asd.txt"; // the file name saved by the Client: String filepath = server. mappath ("download/" + filename); // 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 () ;}public void meth3 () {string filename = "aaa.txt"; // the file name saved by the Client: String filepath = server. mappath ("download/" + filename); // 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 () ;}} public void meth4 () {string filename = "aaa.txt"; // string filepath = server. mappath ("download/" + filename); // 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 ();}

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.