Microsoft Knowledge Base Email Alertz

(812406) - When you try to use the Response.WriteFile method to download a large file, the download may not respond, and then you may receive one of the following error messages: The page cannot be displayed -or- Server Application Unavailable The Web...

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 812406 - Last Review: November 21, 2007 - Revision: 4.3

PRB: Response.WriteFile cannot download a large file

Notice
The third-party products that are discussed in this article are manufactured by companies that are independent of Microsoft. Microsoft makes no warranty, implied or otherwise, regarding the performance or reliability of these products.

On This Page

SYMPTOMS

When you try to use the Response.WriteFile method to download a large file, the download may not respond, and then you 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 receive the following message in the Application event log:

Aspnet_wp.exe (or W3wp.exe, for applications that run on Microsoft Internet Information Services [IIS] 6.0) stopped unexpectedly.

During this process, you may also notice an increase in the memory utilization of the Web server.

CAUSE

The hardware configuration of the Web server computer determines the maximum file size that you can successfully download. When the ASP.NET worker process (Aspnet_wp.exe, or W3wp.exe for applications that run on Internet Information Services 6.0 [IIS]) runs the file download request, the file download dialog box appears. The ASP.NET worker process starts to send the data to the Microsoft Internet Information Services Process (Inetinfo.exe or Dllhost.exe). It does not wait for you to click OK.

Depending on the configuration of the computer, the IIS Process may process the data, or the data may be buffered in memory. When the file is large, the data is buffered in memory during communication between these two processes. This may cause an increase in the memory utilization on the server. The error occurs because of the memory constraints on the Web server.

WORKAROUND

To work around this issue, use any one of the following methods:
  • Obtain the data in small portions, and then move the data to the output stream for download. The following code demonstrates how to do this.

    Important When you set the value of the debug attribute of the compilation element to false in the Web.config file of your ASP.NET application, you must set the Server.ScriptTimeout property to an appropriate value for the file download size. By default, the Server.ScriptTimeout value is set to 90 seconds. However, when the debug attribute is set to true, the Server.ScriptTimeout value will be set to a very large 30,000,000 seconds. As a developer, you must be aware of the impact that this may have on the behavior of your ASP.NET Web application.

    Also, in the code that follows you must be aware of the parameter values that are used with the FileStream constructor. The enumeration values that are specifed make a significant impact on the functionality that is provided. For more information, refer to the FileStream link in the REFERENCES section.
    Visual Basic .NET Code
          Dim iStream As System.IO.Stream
    
          ' Buffer to read 10K 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 dataToRead > 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
             Response.Close()
          End Try
    
    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();
    		}
    		Response.Close();
    	}
    	
    Replace DownloadFileName with the name of a file that is larger than 100 megabytes (MB).

    -or-
  • Provide a link for the user to download the file.

    -or-
  • Use Microsoft ASP 3.0 for the downloads or use Software Artisans FileUp with ASP.

    -or-
  • Create an ISAPI extension to download the file.

    -or-
  • Use FTP to download the file.

STATUS

This behavior is by design.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. In Microsoft Visual Basic .NET or in Microsoft Visual C# .NET, create a new Web Application project. By default, WebForm1.aspx is created.
  2. Drag a button object from the tool box to WebForm1.aspx.
  3. Double-click the button object to open the Click event in Code View.
  4. Paste the following code in 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(filepath)

    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 a file that is larger than 100 MB.
  6. On the Debug menu, click Start.
  7. Click Button1.

REFERENCES

For more information, click the following article numbers to view the articles in the Microsoft Knowledge Base:
307603   (http://kbalertz.com/Feedback.aspx?kbNumber=307603/ ) How to write binary files to the browser using ASP.NET and Visual Basic .NET
306654   (http://kbalertz.com/Feedback.aspx?kbNumber=306654/ ) How to write binary files to the browser using ASP.NET and Visual C# .NET
For more information, visit the following Microsoft Developer Network (MSDN) Web site:
http://msdn2.microsoft.com/en-us/library/system.io.filestream(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/system.io.filestream(vs.71).aspx)

APPLIES TO
  • Microsoft Web Services Enhancements for Microsoft .NET 1.1
  • Microsoft ASP.NET 1.1
  • Microsoft ASP.NET 1.0
Keywords: 
kbwebserver kbwebforms kbweb kbdownload kbprb KB812406
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Victor - victor.gatnicki NOSPAM-AT-NOSPAM ualberta.ca Report As Irrelevant  
Written: 2/23/2004 9:16 PM
I have memory problems using this code to stream files to users, any suggestions.

Anonymous User Reported as Irrelevant  
Written: 11/5/2004 2:24 PM
Great article...fixed my problem. One of the only times you can cut and paste code, and it actually works.

Kavitha Shalini Reported as Irrelevant  
Written: 11/22/2004 6:30 PM
Use Response.TransmitFile for larger files.

Priya - pchandekar NOSPAM-AT-NOSPAM fcsltd.com Reported as Irrelevant  
Written: 12/20/2004 7:08 AM
When I use the above code for downloading the files to client m/c, while opening the file it gives me error "Cannot find the :\Documents and Settings\pchandekar\Local Settings\Temporary Internet Files\Content\IES\FRWU9Z2A\20122004132919mphtml.txt file Do you want to create a new file?", but if I save the file , it saves it properly. Any guesses..why I getting this error ??

Anonymous User - amadouN NOSPAM-AT-NOSPAM gmail.com Reported as Irrelevant  
Written: 1/24/2005 3:03 PM
thanks for the code

lp Reported as Irrelevant  
Written: 4/9/2005 4:40 PM
Been looking for working code to do this for awhile. You can use this technique to protect files as well. Place your "protected" files offsite (unreachable directly by URL, non web folder), and use a httpmodule or similar to transfer those files to the client upon request.

UdhayaSankar Reported as Irrelevant  
Written: 9/14/2005 12:29 PM
Great Article and really helpful. Alternatively you can Use Response.TransmitFile for larger files download. (It does the same as above code does).