I'm really baffled because on one website, my code works perfectly, and on another website, it doesn't.
The file downloads without the extension, but when I rename the downloaded file to include the extension (I add .pdf
to the filename), it opens correctly as a PDF. I am 100% sure bytes
and filename
are correct, and filename
is report.pdf
.
Here's the original code:
private void downloadByteStreamAsFile(Byte[] bytes, String fileName)
{
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.Clear();
//response.Flush(); //comment this or else no file returned
response.AddHeader("Content-Type", "binary/octet-stream");
response.AddHeader("Content-Disposition",
"attachment; filename=" + fileName + "; size=" + bytes.Length.ToString());
response.BinaryWrite(bytes);
response.Flush();
response.End();
}
}
I also tried:
- using output stream Trying to stream a PDF file with asp.net is producing a "damaged file"
- removing the space after the semicolon in
Content-Disposition
file extensions lost between browsers in asp.net c# application "attachment; filename=" + fileName + ".PDF; size=" + bytes.Length.ToString());
(so now the file should be namedreport.pdf.PDF
but it is still only namedreport
)
Please help
Update: Code works fine in IE and Chrome, only Firefox has this issue of losing the file extension