简体   繁体   English

在浏览器中打开PDF而不是下载它

[英]Opening a PDF in browser instead of downloading it

I'm using iTextSharp to print a panel into PDF on button click. 我正在使用iTextSharp在按钮点击时将面板打印成PDF。 After clicking on the button, the PDF is downloading to the client's computer. 单击按钮后,PDF将下载到客户端的计算机。 Instead of this I need the PDF to be opened in a browser instead of downloading. 而不是这个我需要在浏览器中打开PDF而不是下载。 From the browser the user will be able to download the PDF to his PC. 用户可以从浏览器将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();

Change the content-disposition to inline instead 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. 了解更多详情。

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">

You should look at the "Content-Disposition" header; 您应该查看“Content-Disposition”标题; 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. 例如,将“Content-Disposition”设置为“attachment; filename = FileName.pdf”将提示用户(通常)使用“另存为:FileName.pdf”对话框,而不是打开它。 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. 但是,ASP.NET为此提供了Response.TransmitFile。 For example (assuming you aren't using MVC, which has other preferred options): 例如(假设您没有使用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中的文件将流转换为bytesarray然后填写内容

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... 我想它会帮助你......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM