locked
HttpHandler doesn't allow user to download large files RRS feed

  • Question

  • User-2126497664 posted

    Let me start off by saying I'm only mildly sure the cause of this is the file size.

    I have an HttpHandler that pushes a file to the user. When the file is less than ~30MB everything works fine. As soon as the file gets larger, then the page doesn't load properly and instead displays this message: "Internet Explorer cannot display the webpage".

    When I run the site locally (through VS) then large files don't fail. This is only a problem once everything is on the server. I also don't have the luxury of debugging remotely which is why this is such a challenge.

    Here is my code just in case it might help:

    public class ReportDownloader : IHttpHandler
    {
        private string reportName;
        private string fileName;
        private string filePath;
    
        public bool IsReusable { get { return false; } }
    
        public void ProcessRequest(HttpContext context)
        {
            if (context.Request["report"] != null)
            {
                reportName = context.Request["report"].Decrypt();
                fileName = reportName + ".docx";
                filePath = string.Format("{0}\\{1}\\{2}", Config.ReportsPath, reportName, fileName);
            }
    
            if (File.Exists(filePath))
            {
                // Set the content type, append the header and the output the file.
                context.Response.ContentType = fileName.GetContentType();
                context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
                context.Response.WriteFile(filePath);
            }
            else
            {
                // If no file was found, alert the user.
                context.Response.Write("<script language=JavaScript>alert('The file is currently unavailable.'); window.close();</script>");
            }
        }
    }


     

    Tuesday, December 8, 2009 7:13 PM

Answers

  • User-2126497664 posted

    Actually, the fix was amazingly simple. Referencing the line numbers above, if I change line 23 from:

    context.Response.WriteFile(filePath);


    to:

    context.Response.WriteFile(filePath, true);


    then everything works fine! Apparently I just had to ensure I was reading it into memory first, though, this seems quite obscure. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 9, 2009 10:55 AM

All replies

  • User-748361252 posted

    Please add following in your web.config file.

    <system.web>

    <httpRuntime maxRequestLength="102400"/> 

    </system.web>


    it means that maximum size of your page can be 100 MB.

    For more details, Please view the following link.

    http://support.microsoft.com/?id=295626


    Hope it will help. 

    Tuesday, December 8, 2009 8:01 PM
  • User-2126497664 posted

    Nope, didn't do it. Is that solution really supposed to apply to downloads as well? 

    Tuesday, December 8, 2009 8:10 PM
  • User-748361252 posted

    Yes, because that downloaded file is part of page response.  For example; if downloaded file size is 50 MB, your page response will be 50+ MB. so it should work in this scenario.

    Please check your event log and see if you find any error there???


    Also you can try executionTimeout="3000" as follows.

    <httpRuntime executionTimeout="3000" maxRequestLength="102400"/>


    it indicates the maximum number of seconds a request is allowed to execute before being automatically shut down by ASP.NET.

    May be your request is taking too much time and that's why it get timeout.

    Tuesday, December 8, 2009 8:39 PM
  • User-2126497664 posted

    Actually, the fix was amazingly simple. Referencing the line numbers above, if I change line 23 from:

    context.Response.WriteFile(filePath);


    to:

    context.Response.WriteFile(filePath, true);


    then everything works fine! Apparently I just had to ensure I was reading it into memory first, though, this seems quite obscure. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 9, 2009 10:55 AM
  • User555650869 posted

    Brilliant! I was banging my head on the table for over a day with this same problem. Thanks for posting this solution.

    Wednesday, December 23, 2009 2:31 PM