C# 将 PDF 流式传输到浏览器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16193960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Stream PDF to browser?
提问by user2315877
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!!!
谁能告诉我如何将 pdf 流式传输到新的标签浏览器?我只有内存中的 pdf 流,当我单击链接以在新选项卡或窗口浏览器中显示 PDF 时,我想要。我怎么能那样做?谢谢!!!
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:
在我后面的代码中,我在 onload 事件中有这个:
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)
以某种方式我必须处理流pdf(IFile包含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!
我有另一个问题,无法正常工作 OnClientClick="aspnetForm.target='_blank';" timbck2 建议我。我必须在新窗口中打开文件(图像或 pdf ),我该怎么办?不工作这个。谢谢!
Timbbck2 my asp code is:
Timbbck2 我的 asp 代码是:
<asp:LinkButton runat="server" ID="LinkButtonFile" OnClick="LinkButtonFile_Click" OnClientClick="aspnetForm.target = '_blank';"></asp:LinkButton>
Thanks!!!
谢谢!!!
采纳答案by timbck2
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.
我已经从磁盘上的文件中完成了这项工作 - 从内存流发送数据应该没有太大的不同。除了数据本身之外,您还需要向浏览器提供两条元数据。首先,您需要提供数据的 MIME 类型(在您的情况下是application/pdf),然后在 Content-Length 标头中提供以字节(整数)为单位的数据大小,然后发送数据本身。
So in summary (taking into account the comments and what I thinkyou'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):
后面的代码中的这几行 C# 代码应该可以解决问题(或多或少):
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();
}
回答by alex
To display pdf directly in the browser you should set content type for the response to application/pdf
要在浏览器中直接显示 pdf,您应该为响应设置内容类型 application/pdf
Your user should also have some kind of pdf reader installed for this to work.
您的用户还应该安装某种 pdf 阅读器才能正常工作。
回答by Jakob Gade
Did you try Response.BinaryWrite?? You probably want to set some headers as well.
你试过Response.BinaryWrite吗??您可能还想设置一些标题。
回答by CularBytes
The top answer suggests converting (casting) a stream to a byte[] first, that will download the file to the server (from whatever stream it is coming from) and then send it to the client.
最佳答案建议首先将流转换(转换)为 byte[],这会将文件下载到服务器(从它来自的任何流),然后将其发送到客户端。
If you need to provide a Streamas a parameter for some external service (like me for Azure Storage) you can use the following method.
如果您需要Stream为某些外部服务(如我为 Azure 存储)提供一个作为参数,您可以使用以下方法。
public ActionResult Coupons(string file)
{
Response.AddHeader("Content-Disposition", "inline; filename=" + file);
Response.ContentType = "application/pdf";
AzureStorage.DownloadFile(file, Response.OutputStream);//second parameters asks for a 'Stream', you can use whatever stream you want here, just put it in the Response.OutputStream
return new EmptyResult();
}
That will Stream the file to the client directly (as far as I know).
这将直接将文件流式传输到客户端(据我所知)。

