9

不确定如何准确地表达这个问题......所以欢迎编辑!无论如何......这里去。

我目前正在使用 Crystal Reports 生成 Pdfs,并将输出流式传输给用户。我的代码如下所示:

System.IO.MemoryStream stream = new System.IO.MemoryStream();

stream = (System.IO.MemoryStream)this.Report.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);

this.Response.Clear();
this.Response.Buffer = true;
this.Response.ContentType = "application/pdf";
this.Response.BinaryWrite(stream.ToArray());
this.Response.End();

此代码运行后,它将 Pdf 流式传输到打开 Acrobat Reader 的浏览器。效果很好!

我的问题是当用户尝试保存文件时,它默认为实际文件名......在这种情况下,它默认为 CrystalReportPage.pdf。无论如何我可以设置这个吗?如果是这样,怎么做?

任何帮助,将不胜感激。

4

2 回答 2

17

通常,通过 content-disposition 标头 - 即

Content-Disposition: inline; filename=foo.pdf

因此,只需使用 Headers.Add 或 AddHeader 或其他任何内容:

response.Headers.Add("Content-Disposition", "inline; filename=foo.pdf");

(内联是“在浏览器中显示”,附件是“另存为文件”)

于 2008-12-06T18:07:17.000 回答
7
System.IO.MemoryStream stream = new System.IO.MemoryStream();

stream = (System.IO.MemoryStream)this.Report.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);

this.Response.Clear();
this.Response.Buffer = true;
this.Response.ContentType = "application/pdf";
this.Response.AddHeader("Content-Disposition", "attachment; filename=\""+FILENAME+"\"");
this.Response.BinaryWrite(stream.ToArray());
this.Response.End();
于 2008-12-06T18:10:20.253 回答