Response. writefile cannot download large files

Source: Internet
Author: User
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 compile the elementDebugSet the property valueFalseYou must setServer. scripttimeoutSet the property to an appropriate value. By default,Server. scripttimeoutThe value is set to 90 seconds. However, whenDebugProperty is setTrue,Server. scripttimeoutThe 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 knowFilestreamThe parameter value used by the constructor. The specified enumerated value has a major impact on the provided functions. For more information, seeFilestreamLink.

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

SetDownloadfilenameReplace it with the name of the file 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.