Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the name of the file when streaming a Pdf in a browser?

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.

like image 221
mattruma Avatar asked Dec 06 '08 18:12

mattruma


People also ask

What is the file name for PDF?

PDF stands for "portable document format". Essentially, the format is used when you need to save files that cannot be modified but still need to be easily shared and printed.


2 Answers

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")

like image 125
Marc Gravell Avatar answered Sep 29 '22 19:09

Marc Gravell


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();
like image 42
mmx Avatar answered Sep 29 '22 18:09

mmx