Error handling when downloading file from ASP.NET Web Handler (.ashx)

13,062

Solution 1

First of all on this answer of mine the user ask how to make the download with javascript. But if you have the link you do not necessary need to add it to javascript, you can add it also using a regular download link.

Now many web sites use a page with some text that says "and now your download will start in few seconds. If not start please click here bla bla bla". One of the reasons is similar to yours, the user must know that something will start the download in few seconds and if not, then the user understand that there is a problem with out other extra message.

In general when you going to send a file for download, you are not in the page any more, you can not send message in the middle of the download, - or when the file ends with pdf, then is probably not going to show anything on a web page. So from my point of view if you do have issues and throw exceptions, you need to design some steps on how you inform the user that something was going wrong - like the middle page that inform the user that "now you going to receive a file - if the file not start the download in few seconds please do that"

However, when there is an exception thrown in the handler they are redirected to the URL of the handler and shown a blank page.

To avoid that black page, on error you can return that code and the user is stay on page as it is, but still did not get any error message.

Response.TrySkipIisCustomErrors = true;
Response.Status = "204 No Content";
Response.StatusCode = 204;
Response.End();

Alternative on exception you can try to redirect him to an error page using the

Response.Redirect("errorpage.aspx");

and maybe you can send and some error ids there.

You can also try to just show a simple message as

context.Response.ContentType = "text/html";
context.Response.Write("Error downloading file - Please contact with us");

Solution 2

How about instead of setting the href of the window, set the src of a hidden iframe. You can then do some trickery with the returned value in the hidden iframe, such as:

context.Response.Clear();
context.Response.Write("<script>alert('" + GetErrorMessage(e) + "');</script>");

This crude example would just alert the error message. If you're on the same domain (which I suspect you are!) then you can get more clever with, instead of alerting, calling a method in the parent, e.g.

context.Response.Clear();
context.Response.Write("<script>window.parent.someFunction('" + GetErrorMessage(e) + "');</script>");

Here's an example I found on SO doing just this: Retrieving HTTP status code from loaded iframe with Javascript

It's for an upload, not a download, but same concept.

Share:
13,062
Steve
Author by

Steve

Senior Software Engineer with 20 years of web &amp; mobile development experience.

Updated on June 14, 2022

Comments

  • Steve
    Steve almost 2 years

    I have a web page which a user can download a PDF file via an ASP.NET web handler (.ashx). It is implemented like the answer in this question. The problem I have is when I do do this window.top.location.href = url; in my JavaScript I have no real control over what happens if there is an exception thrown in the handler. The user experience, when everything works correctly, is that they essentially stay on the page they were on and the browser tells them that they can download the PDF file. However, when there is an exception thrown in the handler they are redirected to the URL of the handler and shown a blank page.

    Here is some example code to make it more clear:

    JavaScript:

    function openPDF() {
        var url = GeneratePDFUrl();
        window.top.location.href = url;
    }
    

    Handler:

    public override void Process(HttpContext context)
    {
        byte[] pdfdata = GetPDFData();
        context.Response.ContentType = "application/pdf";
        context.Response.AddHeader("content-disposition", "attachment; filename=\"" + GetPDFFileName() + "\"");
        context.Response.AddHeader("content-length", pdfdata.Length.ToString());
        context.Response.BinaryWrite(pdfdata);
    }
    

    The problem happens when GetPDFData() throws an exception. We are doing what we can to prevent GetPDFData() from throwing an exception, but it is generated from user input so we're also handling it here in case there are cases we don't/can't predict which generate an error.

    Here is one solution I have come up with, but it shows the user error text (instead of a blank page)

    public override void Process(HttpContext context)
    {
        try
        {
            byte[] pdfdata = GetPDFData();
            WritePDF(pdfdata, GetPDFFileName()); // Executes code in example above
        }
        catch (Exception e)
        {
            context.Response.Clear();
            context.Response.Write(GetErrorMessage(e));
        }
    }
    

    Ideally I would like to show the user a popup so they stayed on the page they were.

  • Steve
    Steve about 11 years
    Thanks for the response - your Response.Redirect was the answer we went with.