【问题标题】:Download PDF from ReportViewer从 ReportViewer 下载 PDF
【发布时间】:2015-09-02 18:48:59
【问题描述】:

我有一个“下载 PDF”按钮,它呈现 ReportViewer 并使用 Adob​​e Reader 在浏览器中打开它。
代码如下:

Warning[] warn = null;
String[] streamids = null;
String mimeType = "application/pdf";
String encoding = String.Empty;
String extension = String.Empty;
Byte[] byteViewer;
byteViewer = report.LocalReport.Render("pdf", null, out mimeType, out encoding, out extension, out streamids, out warn);
Response.Buffer = true;
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "inline; filename=" + fileName + ".pdf");
Response.BinaryWrite(byteViewer);
Response.Flush();
Response.End();

但是现在,我不想打开 pdf,而是想打开一个下载对话框来下载文件。我应该对代码进行哪些更改?

@编辑 该问题的答案太广泛了,我的问题是为了简化下载 ReportViewer 的过程,并为未来可能遇到此问题的 C# 程序员添加参考,而无需挖掘无用的 php 代码行来查找答案。

【问题讨论】:

  • HTTP Headers for File Downloads 的可能重复项
  • 那个骗子是针对php的,但是php是无关紧要的。标题很重要。
  • 你在那个问题中看到有很多行“标题”代码,如果你能简单地说哪一个解决了我的问题,那就太好了......
  • 没关系,找到了。

标签: c# pdf crystal-reports


【解决方案1】:

为将来可能会发现此问题的 StackOverflow 用户简化代码:

Response.AddHeader("content-disposition", "inline; filename=" + fileName + ".pdf"); 中,只需将inline 更改为attachment,这将下载文件而不是在浏览器中打开。

这个答案太简单了,所以为了公平起见,我会检查“社区维基”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-05
    • 2021-01-09
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    • 2016-01-03
    • 2015-06-17
    • 2010-12-02
    相关资源
    最近更新 更多