Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9210749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:08:52+00:00 2026-06-18T01:08:52+00:00

I have a web page which a user can download a PDF file via

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T01:08:54+00:00Added an answer on June 18, 2026 at 1:08 am

    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");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a tag field in my web page, in which user can enter
I have a user profile page on my web app which has contact information.
In my web site I have a handler (GetFile.ashx) which, when executed, lets you
I have web page which is passing a querystring parameter to page 2: <a
I have a web page which uses in PHP and a jQuery DataTable to
I have web page in PHP which displays all records in a table. I
I have a web page in which a fair amount of the content is
I have a web page on which i am applying Ajax enabled update progress
I have a Webforms, AJAX-enabled web page which, when rendering large amounts of data,
I have a web page that displays dynamic data which changes every 2 seconds.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.