如何流在浏览器中的PDF文件时设置文件的名称?文件、器中、名称、PDF

2023-09-04 02:44:37 作者:青鸳如烟故人念

不知道到底如何字这个问题......所以编辑都欢迎!反正......这里去。

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

我目前使用Crystal Reports生成的PDF文件,只是流输出给用户。我的code如下所示:

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

在此code运行它流的PDF浏览器开放Acrobat Reader软件。伟大的作品!

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

当用户试图在文件则默认它默认保存到实际的文件名...在这种情况下CrystalReportPage.pdf我的问题是。反正我有可以设置呢?如果是这样,怎么样?

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?

任何帮助将是AP preciated。

Any help would be appreciated.

推荐答案

通常情况下,通过内容部署头 - 即

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

Content-Disposition: inline; filename=foo.pdf

因此​​,只要使用Headers.Add,或AddHeader的,或不管它是什么,有:

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

response.Headers.Add(内容处置,内联;文件名= foo.pdf);

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

(内联是展现在浏览器,附件另存为文件)

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