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.
Add a content-disposition to the header:
Response.AddHeader("content-disposition", @"attachment;filename=""MyFile.pdf""");
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With