没有文件时PDFHandler.ash错误

发布于 2024-12-04 11:21:36 字数 1197 浏览 1 评论 0原文

我的 ASHX 处理程序在生成 PDF 时遇到问题。

当用户点击“查看 PDF”按钮时,它将在数据库中查找 PDF 文件并显示它,但如果那里没有 PDF 文件,它应该显示一个空白页,指出“没有可用的 PDF”,但相反我在这行代码上收到“空引用”错误:

ms.WriteTo(context.Response.OutputStream)

下面是处理程序的代码:

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
    'This class takes the uniqueidentifier of an image stored in the SQL DB and sends it to the output stream
    'This saves storing copies of image files on the web server as well as in the DB
    context.Response.Clear()
    If context.Request.QueryString("fileSurveyID") IsNot Nothing Then
        Dim filesID As String = context.Request.QueryString("fileSurveyID")
        Dim fileName = String.Empty
        Dim ms As MemoryStream = GetPDFFile(filesID)
        context.Response.ContentType = "application/pdf"
        context.Response.AddHeader("Content-Disposition", "attachment;filename=" & fileName)
        context.Response.Buffer = True
        ms.WriteTo(context.Response.OutputStream)
        context.Response.End()
    Else
        context.Response.Write("<p>No pdf file</p>")
    End If
End Sub

谁能告诉我如何消除此错误?

I am having an issue with my ASHX handler that is generating PDF.

When the user hits a "View PDF" button, it will look in the database for the PDF file and display it, but if there isn't a PDF file there it should display a blank page saying "no PDF available", but instead I get a "null reference" error on this line of code:

ms.WriteTo(context.Response.OutputStream)

Below is the code for the handler:

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
    'This class takes the uniqueidentifier of an image stored in the SQL DB and sends it to the output stream
    'This saves storing copies of image files on the web server as well as in the DB
    context.Response.Clear()
    If context.Request.QueryString("fileSurveyID") IsNot Nothing Then
        Dim filesID As String = context.Request.QueryString("fileSurveyID")
        Dim fileName = String.Empty
        Dim ms As MemoryStream = GetPDFFile(filesID)
        context.Response.ContentType = "application/pdf"
        context.Response.AddHeader("Content-Disposition", "attachment;filename=" & fileName)
        context.Response.Buffer = True
        ms.WriteTo(context.Response.OutputStream)
        context.Response.End()
    Else
        context.Response.Write("<p>No pdf file</p>")
    End If
End Sub

Can anyone tell me how to get rid of this error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初懵 2024-12-11 11:21:36

简单的 If..Then 应该可以解决问题:

Dim ms As MemoryStream = GetPDFFile(filesID)
If ms IsNot Nothing Then
    context.Response.ContentType = "application/pdf"
    context.Response.AddHeader("Content-Disposition", "attachment;filename=" & fileName)
    context.Response.Buffer = True
    ms.WriteTo(context.Response.OutputStream)
    context.Response.End()
End If

Simple If..Then should do the trick:

Dim ms As MemoryStream = GetPDFFile(filesID)
If ms IsNot Nothing Then
    context.Response.ContentType = "application/pdf"
    context.Response.AddHeader("Content-Disposition", "attachment;filename=" & fileName)
    context.Response.Buffer = True
    ms.WriteTo(context.Response.OutputStream)
    context.Response.End()
End If
如痴如狂 2024-12-11 11:21:36

您可能希望将以下内容移出 if:

context.Response.End()

以便它每次都会执行。

但是,您说以下行在没有可用 PDF 时执行:

ms.WriteTo(context.Response.OutputStream)

这表明您的 if 条件有问题

You probably want to move the following out of the if:

context.Response.End()

so it execute each time regardless.

However you say the following line executes when no PDF available:

ms.WriteTo(context.Response.OutputStream)

which would suggest something wrong with your if condition

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文