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

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 = 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:


function openPDF() {
    var url = GeneratePDFUrl(); = url;

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

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)
        byte[] pdfdata = GetPDFData();
        WritePDF(pdfdata, GetPDFFileName()); // Executes code in example above
    catch (Exception e)

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


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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;

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

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");

Method 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.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.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.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x