Response. writefile cannot download large files

Source: Internet
Author: User

Class = sbody> when you try to use response. when the writefile method is used to download a large file, the download operation may not respond, and subsequently may receive one of the following error messages: the page cannot be displayed-or-server application unavailable

The Web application you are attempting to access on this web server is currently unavailable. Please hit the "refresh" button in your web browser to retry your request.

Administrator Note: an error message detailing the cause of this specific request failure can be found in the system event log of the Web server. please review this log entry to discover what caused this error to occur. you may also see the following message in the Application Event Log:
Aspnet_wp.exe (w3wp.exe for applications running on Microsoft Internet Information Service [IIS] 6.0) unexpectedly stops.

In this process, you may also find that the memory usage of the Web server increases. Back to Top

Cause
The hardware configuration of the Web server computer determines the maximum file size that you can successfully download. The file download dialog box appears when the ASP. NET assist (aspnet_wp.exe) is executed for applications running on Internet Information Service 6.0 [IIS], w3wp.exe. The ASP. NET auxiliary process starts to send data to Microsoft Internet Information Service (inetinfo.exe or dllhost.exe. It does not start sending after you click OK.

According to the computer configuration, the IIS process may process data or cache the data in the memory. If the file is too large, data will be cached in the memory when the two processes communicate with each other. This may increase the memory usage on the server. The cause of this error is the memory limit on the Web server. Back to Top

Alternative Method
To solve this problem, use either of the following methods: divide the data into smaller parts and move it to the output stream for download to obtain the data. The following code demonstrates how to complete this operation.

Important: when you are in ASP. NET Application Web. when the debug attribute value of the compilation element is set to false in the config file. set the scripttimeout attribute to an appropriate value. By default, the server. scripttimeout value is set to 90 seconds. However, when the debug attribute is set to true, the server. scripttimeout value is set to a very large value (30,000,000 seconds ). As a developer, you must be aware of the potential impact on the behavior of your asp. NET web application.

In the following code, you must also know the parameter values used with the filestream constructor. The specified enumerated value has a major impact on the provided functions. For more information, see the filestream link in the Section.
Visual Basic. Net code
Dim istream as system. Io. Stream

'Buffer to read 10 K bytes in Chunk:
Dim buffer (10000) as byte

'Length of the file:
Dim length as integer

'Total bytes to read:
Dim datatoread as long

'Identify the file to download including its path.
Dim filepath as string = "downloadfilename"

'Identify the file name.
Dim filename as string = system. Io. Path. getfilename (filepath)

Try
'Open the file. istream = new system. io. filestream (filepath, system. io. filemode. open, _ IO. fileaccess. read, Io. fileshare. read) 'total bytes to read: datatoread = istream. length response. contenttype = "application/octet-stream" response. addheader ("content-disposition", "attachment; filename =" & filename) 'read the bytes. while oreoread> 0' verify that the client is connected. if response. isclientconnected then' read the data in buffer length = istream. read (buffer, 0, 10000) 'write the data to the current output stream. response. outputstream. write (buffer, 0, length) 'flush the data to the HTML output. response. flush () redim buffer (10000) 'Clear the buffer datatoread = datatoread-length else' prevent infinite loop if user disconnects datatoread =-1 end if end while catch ex as exception 'trap the error, if any. response. write ("error:" & Ex. message) Finally if isnothing (istream) = false then 'close the file. istream. close () end if end try Visual C #. net code
System. Io. Stream istream = NULL;

// Buffer to read 10 K bytes in Chunk:
Byte [] buffer = new byte [10000];

// Length of the file:
Int length;

// Total Bytes to read:
Long datatoread;

// Identify the file to download including its path.
String filepath = "downloadfilename ";

// Identify the file name.
String filename = system. Io. Path. getfilename (filepath );

Try
{
// Open the file.
Istream = new system. Io. filestream (filepath, system. Io. filemode. Open,
System. Io. fileaccess. Read, system. Io. fileshare. Read );

// Total Bytes to read:
Datatoread = istream. length;

Response. contenttype = "application/octet-stream ";
Response. addheader ("content-disposition", "attachment; filename =" + filename );

// Read the bytes.
While (datatoread> 0)
{
// Verify that the client is connected.
If (response. isclientconnected)
{
// Read the data in buffer.
Length = istream. Read (buffer, 0, 10000 );

// Write the data tothe current output stream. response. outputstream. write (buffer, 0, length); // flush the data to the HTML output. response. flush (); buffer = new byte [10000]; datatoread = datatoread-length;} else {// prevent infinite loop if user disconnects datatoread =-1 ;}}} catch (exception ex) {// trap the error, if any. response. write ("error:" + ex. message);} finally {If (istream ! = NULL) {// close the file. istream. Close () ;}} replace downloadfilename with the name of the file larger than 100 MB.

-Or -? Provide users with links to download files.

-Or-
? Download with Microsoft ASP 3.0 or use software artisans fileup with ASP.

-Or-
? Create an ISAPI extension to download an object.

-Or-
? Use ftp to download files.
Back to Top

Status
This is caused by design. Back to Top

More information
Steps to reproduce the problem
1. Create a web application project in Microsoft Visual Basic. Net or Microsoft Visual C #. net. By default, webform1.aspx is created.
2. Drag a button object from the toolbox to webform1.aspx.
3. Double-click the button object to open the click event in the Code view.
4. paste the following code into the button1 click event.

Visual Basic. Net code
'Identify the file to download including its path.
Dim filepath as string = downloadfilename

'Identify the file name.
Dim filename as string = system. Io. Path. getfilename (filepath)

Response. Clear ()

'Specify the type of the downloadable file.
Response. contenttype = "application/octet-stream"

'Set the default file name in the filedownload dialog box.
Response. addheader ("content-disposition", "attachment; filename =" & filename &"""")

Response. Flush ()

'Download the file.
Response. writefile (Fi
Lepath)
Visual C #. Net code
// Identify the file to download including its path.
String filepath = downloadfilename;

// Identify the file name.
String filename = system. Io. Path. getfilename (filepath );

Response. Clear ();

// Specify the type of the downloadable file.
Response. contenttype = "application/octet-stream ";

// Set the default file name in the filedownload dialog box.
Response. addheader ("content-disposition", "attachment; filename =" + filename );

Response. Flush ();

// Download the file.
Response. writefile (filepath); 5. Replace downloadfilename with the name of the file larger than 100 MB. 6. On the Debug menu, click Start ". 7. Click "button1"
Article Source: feino Network (www.firnow.com): http://dev.firnow.com/course/4_webprogram/asp.net/netjs/20100629/252435.html

 

 

 

 

 

Visual C #. Net code

System.IO.Stream iStream = null;// Buffer to read 10K bytes in chunk:byte[] buffer = new Byte[10000];// Length of the file:int length;// Total bytes to read:long dataToRead;// Identify the file to download including its path.string filepath  = "DownloadFileName";// Identify the file name.string  filename  = System.IO.Path.GetFileName(filepath);try{// Open the file.iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read,System.IO.FileShare.Read);// Total bytes to read:dataToRead = iStream.Length;Response.ContentType = "application/octet-stream";Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);// Read the bytes.  while (dataToRead > 0){// Verify that the client is connected.if (Response.IsClientConnected) {// Read the data in buffer.length = iStream.Read(buffer, 0, 10000);// Write the data to the current output stream.Response.OutputStream.Write(buffer, 0, length);// Flush the data to the HTML output.Response.Flush();buffer= new Byte[10000];dataToRead = dataToRead - length;}else{//prevent infinite loop if user disconnectsdataToRead = -1;}}}catch (Exception ex) {// Trap the error, if any.Response.Write("Error : " + ex.Message);}finally{if (iStream != null) {//Close the file.iStream.Close();}}

Replace downloadfilename with the name of an object larger than 100 MB.

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.