从的ASP.NET Web处理程序(ashx的)下载文件时错误处理

问题描述:

我有一个网页,用户可以通过一个ASP.NET处理程序下载PDF文件( ashx的)。它被实现象在这个答案question.我的问题是,当我这样做 window.top.location.href = URL; 在我的JavaScript我有超过如果抛出的异常会发生什么没有真正的控制在处理程序。用户体验,当一切都正常工作,是他们基本上是保持他们在网页上,浏览器会告诉他们,他们可以下载PDF文件。然而,当有在处理程序抛出的异常,他们将被重定向到处理程序的URL并显示一个空白页。

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.

下面是一些例子code,使之更加明确:

Here is some example code to make it more clear:

JavaScript的:

JavaScript:

function openPDF() {
    var url = GeneratePDFUrl();
    window.top.location.href = 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());
    context.Response.BinaryWrite(pdfdata);
}

在GetPDFData()抛出一个异常的问题发生。我们正在尽我们所能为prevent从抛出一个异常GetPDFData(),但它是从用户的输入有这样的情况我们不这样做/不能$ P $生成所以我们也是在这里的情况下处理它pdict其产生错误。

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.

所有的首先我的这个答案,从而用户问如何使下载使用JavaScript。但是,如果你有链接你没有必要需要将其添加到JavaScript中,你也可以使用普通的下载链接添加。

First of all on this answer of mine the user ask how to make the download with javascript. But the 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 may 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.

在一般的时候你要发送一个文件的下载,你是不是在专页,你不能在下载过程中发送的消息, - 或当文件,PDF结尾,则很可能不会显示的网页上的任何东西。所以从我的角度来看,如果你确实有问题,并抛出异常,你需要设计你如何告知的东西是走错了用户的一些步骤 - 如中间页面,告知的用户现在你要接收文件 - 如果文件没有启动下载的几秒钟,请做

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"

然而,当在它们的处理程序抛出的异常
  重定向到处理程序的URL并显示一个空白页。

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

要避免黑色页面上的错误,你可以返回code和用户停留页面上,因为它是,但还是没有得到任何错误消息。

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

,也许你可以把一些错误ID那里。

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