在 ASPX 中查看 PDF 文件,而不是在 acrobat 中
当用户单击链接时,它会打开一个新窗口(因为 target="_blank")调用 xyz.ASPX 页面,该页面将动态创建 PDF 发票,将其保存到数据库(而不是物理位置)中,然后我需要能够在 ASPX 页面中打开 PDF 文件。
现在发生的情况是,生成 PDF 文件的 aspx 被调用,它完成其工作,然后打开 Acrobat 显示发票 PDF。但是,xyz.aspx 页面是空白的,不显示任何内容。我不喜欢这样,希望 xyz.aspx 显示 PDF 文件。
根据我的研究,如果你想在 ASPX 页面中显示 PDF,你需要一个 iFrame 或自定义控件。但是,由于 PDF 是动态创建的并且不存储在磁盘上,因此如何设置 iFrame 的源。有没有办法将 src 设置为响应对象或其他内存对象?
有一个简单的方法可以做到这一点吗?我不想使用自定义控件。我正在使用 .NET 3.5 和 C# 以及母版页和 CSS 主题,但我已为此页面禁用它。
来源 xyz.aspx:
`<%@ 页面语言="c#" EnableTheming="false"
ContentType="application/pdf" Codebehind="xyz.aspx.cs" AutoEventWireup="True" Inherits="namespace1.xyz" %>
'
xyz.aspx.cs (从数据库获取 PDF 文件,然后写入 Response 对象)
protected void Page_Load(object sender, EventArgs e)
{
byte[] pdfBinaryFile = GetPDFFileFromDB(PDFId);
if(pdfBinaryFile != null)
Response.BinaryWrite(pdfBinaryFile);
}
When a user clicks on a link, it opens a new window (since target="_blank") calls an xyz.ASPX page that will dynamically create an PDF invoice, saving it into a Database (not a physical location) and then I need to be able to open the PDF file within the ASPX page.
Right now whats happening is that the aspx that generates the PDF file is called, it does its stuff but then opens Acrobat to display the invoice PDF. However, the xyz.aspx page is blank and does not display anything. I do not like this and would like xyz.aspx to display the PDF file.
From my research, it seems that if you want to display the PDF within the ASPX page, you need an iFrame or a custom control. However, since the PDF is dynamically created and not stored on disk, how do I set the source for the iFrame. Is there a way to set the src as a response object or some other in-memory object?
Is there an easy way of doing this? I dont want to use a custom control. I am using .NET 3.5 and C# with Master pages and CSS themes but I have disabled it for this page.
Source xyz.aspx:
`<%@ Page language="c#" EnableTheming="false"
ContentType="application/pdf" Codebehind="xyz.aspx.cs" AutoEventWireup="True" Inherits="namespace1.xyz" %>
'
xyz.aspx.cs (gets the PDF file from the DB and then writes to Response object)
protected void Page_Load(object sender, EventArgs e)
{
byte[] pdfBinaryFile = GetPDFFileFromDB(PDFId);
if(pdfBinaryFile != null)
Response.BinaryWrite(pdfBinaryFile);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看自定义处理程序(.ashx 文件)和 Response.Write。
虽然你有点受 Adobe 一时兴起的影响,因为我认为他们的插件在 iframe 中表现不佳。我要研究的一件事是不显示 PDF,而是提供 HTML 预览,然后提供一个链接,以便用户可以下载实际的 PDF。这将是一个更明智的方法,因为没有安装 Acrobat 的人都会看到一个漂亮的“下载 pdf”框,无论它是否在 iframe 中。 (请注意,我说的是 Acrobat。有些人有 PDF 阅读器,但没有适用于其浏览器的 PDF 阅读器插件,例如我自己)
对于 PDF 下载,您需要的只是一个自定义 HTTP 处理程序,该处理程序会发送一个查询字符串来告诉它他们想要下载哪个 PDF。 (因此,不需要将 PDF 写入磁盘)
Look into custom handlers(.ashx files) and Response.Write.
Though your kinda at the whim of Adobe because I don't think their plugin plays nice in iframes. One thing I would look into is instead of displaying the PDF, instead have an HTML preview and then have a link so the user can download the actual PDF. That would be a much more sane way to do it because people without Acrobat installed are going to get a nice "download pdf" box show up no matter if its in an iframe or not. (note I say Acrobat. There are people that have a PDF reader but not a PDF reader plugin for their browser, such as myself)
For the PDF download, all you would need is a custom HTTP handler that gets sent a query string telling it which PDF they want to download. (and thus, would not require writing the PDF to disk)
尝试添加
Content-Disposition: inline
标头:Try adding a
Content-Disposition: inline
header:在这里查找有关使用哪些标头的信息:PHP 标头示例 (我认为内容处置是一项重要的任务)。也许这会有所帮助。
Look for info on what headers to use for this here: PHP header examples (Content-Disposition is one important one I think). Maybe this will help.