将 PDF 流式传输到浏览器?

Stream PDF to browser?(将 PDF 流式传输到浏览器?)

本文介绍了将 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:LinkBut​​ton runat="server" ID="LinkBut​​tonFile" OnClick="LinkBut​​tonFile_Click" OnClientClick="aspnetForm.target = '_blank';"></asp:LinkBut​​ton>

谢谢!!!

解决方案

我已经通过磁盘上的文件完成了此操作 - 从内存流发送数据应该没有太大不同.除了数据本身之外,您还需要向浏览器提供两条元数据.首先,您需要提供数据的 MIME 类型(在您的情况下为 application/pdf),然后在 Content-Length 标头中提供以字节(整数)为单位的数据大小,然后发送数据本身.

总而言之(考虑到评论以及我认为您要问的内容,您的标记将如下所示:

<asp:LinkBut​​ton ID="whatever" runat="server" OnClick="lnkBut​​ton_Click"OnClientClick="aspnetForm.target='_blank';">您的链接文本</aspnet:LinkBut​​ton>

后面的代码中的这几行 C# 代码应该可以解决问题(或多或少):

protected void lnkBut​​ton_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 流式传输到浏览器?

基础教程推荐