Stream PDF to browser?(将 PDF 流式传输到浏览器?)
问题描述
谁能告诉我如何将 pdf 流式传输到新的标签浏览器?我只有内存中的 pdf 流,当我单击链接以在新选项卡或窗口浏览器中显示 PDF 时,我想要.我怎么能那样做?谢谢!!!
我有这个链接:
<a id="hrefPdf" runat="server" href="#" target="_blank"><asp:Literal ID="PdfName" runat="server"></asp:文字></a>
在我后面的代码中,我在 onload 事件中有这个:
流 pdf=getPdf如果(pdf != 空){SetLinkPDF(pdf);}私有无效 SetLinkPDF(IFile pdf){hrefPdf.href = "MyPDF 到浏览器"PdfName.Text = pdf.PdfName;}
我必须以某种方式处理流 pdf(IFile 包含 PDF 的名称、流、元数据等)
当我点击在新浏览器中显示流时,我可以做些什么来处理它?</p>
我有另一个问题,无法使用 OnClientClick="aspnetForm.target='_blank';"timbck2 建议我.我必须在新窗口中打开文件(图像或 pdf ),我该怎么办?不工作这个.谢谢!
Timbbck2 我的 asp 代码是:
<asp:LinkButton runat="server" ID="LinkButtonFile" OnClick="LinkButtonFile_Click" OnClientClick="aspnetForm.target = '_blank';"></asp:LinkButton>
谢谢!!!
我已经通过磁盘上的文件完成了此操作 - 从内存流发送数据应该没有太大不同.除了数据本身之外,您还需要向浏览器提供两条元数据.首先,您需要提供数据的 MIME 类型(在您的情况下为 application/pdf
),然后在 Content-Length 标头中提供以字节(整数)为单位的数据大小,然后发送数据本身.
总而言之(考虑到评论以及我认为您要问的内容,您的标记将如下所示:
<asp:LinkButton ID="whatever" runat="server" OnClick="lnkButton_Click"OnClientClick="aspnetForm.target='_blank';">您的链接文本</aspnet:LinkButton>
后面的代码中的这几行 C# 代码应该可以解决问题(或多或少):
protected void lnkButton_Click(object sender, EventArgs e){Response.ClearContent();Response.ContentType = "申请/pdf";Response.AddHeader("Content-Disposition", "inline; filename=" + docName);Response.AddHeader("Content-Length", docSize.ToString());Response.BinaryWrite((byte[])docStream);Response.End();}
Could anyone please tell me how could I stream a pdf to a new tab browser? I just have the pdf stream on memory and I want when I click over the link to show the PDF in a new tab or window browser. How could I do that? Thank!!!
I have this link:
<a id="hrefPdf" runat="server" href="#" target="_blank"><asp:Literal ID="PdfName" runat="server"></asp:Literal></a>
In my code behind I have this on the onload event:
Stream pdf= getPdf
if (pdf != null)
{
SetLinkPDF(pdf);
}
private void SetLinkPDF(IFile pdf)
{
hrefPdf.href = "MyPDF to the Browser"
PdfName.Text = pdf.PdfName;
}
Someway I have to process the stream pdf (IFile contains Name, Stream,Metadata, etc of the PDF)
What can I do to proccess this and whe I click show the stream at a new browser?
I have another probelm, is not working the OnClientClick="aspnetForm.target='_blank';" timbck2 suggested me. I have to open the file (image or pdf ) in a new window, what could I do? Is not working this. Thank!
Timbbck2 my asp code is:
<asp:LinkButton runat="server" ID="LinkButtonFile" OnClick="LinkButtonFile_Click" OnClientClick="aspnetForm.target = '_blank';"></asp:LinkButton>
Thanks!!!
I've done this from a file on disk - sending the data from a memory stream shouldn't be that much different. You need to supply two pieces of metadata to the browser, in addition to the data itself. First you need to supply the MIME type of the data (in your case it would be application/pdf
), then the size of the data in bytes (integer) in a Content-Length header, then send the data itself.
So in summary (taking into account the comments and what I think you're asking, your markup would look like this:
<asp:LinkButton ID="whatever" runat="server" OnClick="lnkButton_Click"
OnClientClick="aspnetForm.target='_blank';">your link text</aspnet:LinkButton>
These few lines of C# code in your code behind should do the trick (more or less):
protected void lnkButton_Click(object sender, EventArgs e)
{
Response.ClearContent();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "inline; filename=" + docName);
Response.AddHeader("Content-Length", docSize.ToString());
Response.BinaryWrite((byte[])docStream);
Response.End();
}
这篇关于将 PDF 流式传输到浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 PDF 流式传输到浏览器?
基础教程推荐
- 使用 SED 在 XML 标签之间提取值 2022-01-01
- 从 VB6 迁移到 .NET/.NET Core 的最佳策略或工具 2022-01-01
- 创建属性设置器委托 2022-01-01
- C# - 将浮点数转换为整数...并根据余数更改整数 2022-01-01
- 我什么时候应该使用 GC.SuppressFinalize()? 2022-01-01
- 如何使用OpenXML SDK将Excel转换为CSV? 2022-01-01
- 覆盖 Json.Net 中的默认原始类型处理 2022-01-01
- C# - 如何列出发布到 ASPX 页面的变量名称和值 2022-01-01
- Page.OnAppearing 中的 Xamarin.Forms Page.DisplayAlert 2022-01-01
- 当键值未知时反序列化 JSON 2022-01-01