C# 在浏览器中打开 PDF 而不是下载它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12086321/
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
Opening a PDF in browser instead of downloading it
提问by Krishna Thota
I'm using iTextSharp to print a panel into PDF on button click. After clicking on the button, the PDF is downloading to the client's computer. Instead of this I need the PDF to be opened in a browser instead of downloading. From the browser the user will be able to download the PDF to his PC.
我正在使用 iTextSharp 在单击按钮时将面板打印为 PDF。单击按钮后,PDF 正在下载到客户端的计算机。相反,我需要在浏览器中打开 PDF 而不是下载。用户可以从浏览器将 PDF 下载到他的 PC。
I'm using the following code:
我正在使用以下代码:
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
pnl_print.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
sr.Close();
hw.Close();
sw.Close();
采纳答案by Alexis Pigeon
Change the content-dispositionto inlineinstead of attachment.
将 更改content-disposition为inline而不是attachment。
The second line of your snippet would then be
您的代码段的第二行将是
Response.AddHeader("content-disposition", "inline;filename=" + filename + ".pdf");
See Content-Disposition:What are the differences between "inline" and "attachment"?for further details.
请参阅内容处置:“内联”和“附件”之间有什么区别?了解更多详情。
回答by Krunal Mevada
Try This Code :
试试这个代码:
Action:
行动:
Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".pdf");
To open in new Tab/Window:
在新标签页/窗口中打开:
@Html.ActionLink("view pdf", "getpdf", "somecontroller", null,
new { target = "_blank" })
OR
或者
<a href="GeneratePdf.ashx?somekey=10" target="_blank">
回答by Skull
You should look at the "Content-Disposition" header; for example setting "Content-Disposition" to "attachment; filename=FileName.pdf" will prompt the user (typically) with a "Save as: FileName.pdf" dialog, rather than opening it. This, however, needs to come from the request that is doing the download, so you can't do this during a redirect. However, ASP.NET offers Response.TransmitFile for this purpose. For example (assuming you aren't using MVC, which has other preferred options):
您应该查看“Content-Disposition”标题;例如将“内容处理”设置为“附件;文件名=文件名.pdf”将提示用户(通常)使用“另存为:文件名.pdf”对话框,而不是打开它。但是,这需要来自正在执行下载的请求,因此您不能在重定向期间执行此操作。但是,ASP.NET 为此提供了 Response.TransmitFile。例如(假设您没有使用 MVC,它有其他首选选项):
Response.Clear();
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=FileName.pdf");
Response.TransmitFile(Server.MapPath("~/folder/Sample.pdf"));
Response.End();
If you are try to open then the file in apicontroller Convert stream to bytesarray and then Fill the content
如果您尝试打开 apicontroller 中的文件将流转换为字节数组,然后填充内容
HttpResponseMessage result = null;
result = Request.CreateResponse(HttpStatusCode.OK);
FileStream stream = File.OpenRead(path);
byte[] fileBytes = new byte[stream.Length];
stream.Read(fileBytes, 0, fileBytes.Length);
stream.Close();
result.Content = new ByteArrayContent(fileBytes);
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = "FileName.pdf";
I think it will help you...
我想它会帮助你...

