如何在浏览器中流式传输Pdf时设置文件名?

不确定如何说出这个问题…欢迎编辑! 无论如何……这里去了。

我目前使用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。 无论如何我可以设置这个吗? 如果是这样,怎么样?

任何帮助,将不胜感激。

通常,通过内容处置标题 – 即

 Content-Disposition: inline; filename=foo.pdf 

所以只需使用Headers.Add或AddHeader,或者其他任何东西,使用:

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

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

 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();