【发布时间】:2011-09-13 12:23:42
【问题描述】:
我的 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
谁能告诉我如何摆脱这个错误?
【问题讨论】: