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

发布于 2024-07-09 06:42:18 字数 671 浏览 6 评论 0原文

不确定如何确切地表达这个问题......所以欢迎编辑! 无论如何……就这样吧。

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

任何帮助,将不胜感激。

Not sure exactly how to word this question ... so edits are welcomed! Anyway ... here goes.

I am currently use Crystal Reports to generated Pdfs and just stream the output to the user. My code looks like the following:

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

After this code runs it streams the Pdf to the browser opening up Acrobat Reader. Works great!

My problem is when the user tries to save the file it defaults to the actual file name ... in this case it defaults to CrystalReportPage.pdf. Is there anyway I can set this? If so, how?

Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

记忆で 2024-07-16 06:42:18

通常,通过内容处置标头 - 即

Content-Disposition: inline; filename=foo.pdf

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

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

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

Usually, via the content-disposition header - i.e.

Content-Disposition: inline; filename=foo.pdf

So just use Headers.Add, or AddHeader, or whatever it is, with:

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

(inline is "show in browser", attachment is "save as a file")

茶底世界 2024-07-16 06:42:18
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();
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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文