Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying filename for dynamic PDF in asp.net

How can I specify the filename when dumping data into the response stream?

Right now I'm doing the following:

byte[] data= GetFoo(); Response.Clear(); Response.Buffer = true; Response.ContentType = "application/pdf";             Response.BinaryWrite(data); Response.End(); 

With the code above, I get "foo.aspx.pdf" as the filename to save. I seem to remember being able to add a header to the response to specify the filename to save.

like image 604
Josh Bush Avatar asked Sep 16 '08 16:09

Josh Bush


2 Answers

Add a content-disposition to the header:

Response.AddHeader("content-disposition", @"attachment;filename=""MyFile.pdf"""); 
like image 88
Ryan Farley Avatar answered Sep 22 '22 09:09

Ryan Farley


FYI... if you use "inline" instead of "attachment" the file will open automatically in IE. Instead of prompting the user with a Open/Save dialogue.

Response.AppendHeader("content-disposition", string.Format("inline;FileName=\"{0}\"", fileName)); 
like image 42
EMR Avatar answered Sep 20 '22 09:09

EMR