(Response. writefile cannot download large files)

Source: Internet
Author: User
Previously, response. writefile (filename) was used, but it cannot be completely downloaded when a large file is encountered. The biggest problem with this method is that it does not directly throw data to the client, but caches data on the Server (IIS. When the file to be downloaded is large, the server pressure will be great. Although IIS supports 2 GB file download, but when the file is on a lot of M, due to factors such as the server and network, the probability of an exception is quite high. Therefore, you cannot use the above method to download large files. Microsoft recommends the following method to replace it: ■ divide the data into smaller parts, and then move it to the output stream for download to obtain the data. ■ Provide users with links to download files. ■ Download with Microsoft ASP 3.0 or use software artisans fileup with ASP. ■ Create an ISAPI extension to download an object. ■ Use ftp to download files. Reference: http://support.microsoft.com/default.aspx? SCID = KB; ZH-CN; 812406c # related code: copy the public class filedown {public filedown () {/// todo: add the constructor logic here //}/// /// The parameter is a virtual path ////// /// Public static string filenameextension (string filename) {return path. getextension (mappathfile (filename ));}/// /// Obtain the physical address ////// /// Public static string mappathfile (string filename) {return httpcontext. Current. server. mappath (filename );}/// /// Use writefile to download the file. The parameter is the virtual path of the file ////// Public static void downloadold (string filename) {string destfilename = mappathfile (filename); // labelmsg. TEXT = destfilename; If (file. exists (destfilename) {fileinfo Fi = new fileinfo (destfilename); httpcontext. current. response. clear (); httpcontext. current. response. clearheaders (); httpcontext. current. response. buffer = false; // httpcontext. current. response. appendheader ("content-disposition", "attachment; filename =" + httputility. urlencode (path. getfilename (destfilename), system. text. encoding. default); httpcontext. current. response. appendheader ("content-disposition", "attachment; filename =" + httputility. urlencode (path. getfilename (destfilename), system. text. encoding. utf8); httpcontext. current. response. appendheader ("Content-Length", Fi. length. tostring (); httpcontext. current. response. contenttype = "application/octet-stream"; httpcontext. current. response. writefile (destfilename); httpcontext. current. response. flush (); httpcontext. current. response. end ();}}/// /// Use outputstream. Write to download objects in multiple parts. The parameter is the virtual path of the object ////// Public static void download (string filename) {string filepath = mappathfile (filename); // specify the block size long chunksize = 204800; // create a K buffer byte [] buffer = new byte [chunksize]; // The number of read bytes long datatoread = 0; filestream stream = NULL; try {// open the file stream = new filestream (filepath, filemode. open, fileaccess. read, fileshare. read); datatoread = stream. length; // Add the HTTP header httpcontext. current. response. contentty Pe = "application/octet-stream"; httpcontext. current. response. addheader ("content-disposition", "attachement; filename =" + httputility. urlencode (path. getfilename (filepath); httpcontext. current. response. addheader ("Content-Length", datatoread. tostring (); While (datatoread> 0) {If (httpcontext. current. response. isclientconnected) {int length = stream. read (buffer, 0, convert. toint32 (chunksize); HT Tpcontext. current. response. outputstream. write (buffer, 0, length); httpcontext. current. response. flush (); httpcontext. current. response. clear (); datatoread-= length;} else {// prevent client from losing connection to datatoread =-1 ;}} catch (exception ex) {httpcontext. current. response. write ("error:" + ex. message);} finally {If (stream! = NULL) {stream. Close () ;}httpcontext. Current. response. Close ();}}/// /// Use outputstream. Write to download an object in multiple parts. The parameter is the absolute path of the object ////// Public static void downloadfile (string filepath) {// string filepath = mappathfile (filename); // specify the block size long chunksize = 204800; // create a K buffer byte [] buffer = new byte [chunksize]; // The number of read bytes long datatoread = 0; filestream stream = NULL; try {// open the file stream = new filestream (filepath, filemode. open, fileaccess. read, fileshare. read); datatoread = stream. length; // Add the HTTP header httpcontext. current. response. con Tenttype = "application/octet-stream"; httpcontext. current. response. addheader ("content-disposition", "attachement; filename =" + httputility. urlencode (path. getfilename (filepath); httpcontext. current. response. addheader ("Content-Length", datatoread. tostring (); While (datatoread> 0) {If (httpcontext. current. response. isclientconnected) {int length = stream. read (buffer, 0, convert. toint32 (chunksize ); Httpcontext. current. response. outputstream. write (buffer, 0, length); httpcontext. current. response. flush (); // httpcontext. current. response. clear (); buffer = new byte [chunksize]; datatoread = datatoread-length;} else {// prevent client from losing connection to datatoread =-1 ;}} catch (exception ex) {Throw ex; // httpcontext. current. response. write ("error:" + ex. message);} finally {If (stream! = NULL) {stream. Close () ;}httpcontext. Current. response. Close () ;}} reprinted in http://www.cnblogs.com/hulang/archive/2013/02/27/2934640.html

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.