Reputation: 15041
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:
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 named report.pdf.PDF
but it is still only named report
)Please help
Update: Code works fine in IE and Chrome, only Firefox has this issue of losing the file extension
Upvotes: 0
Views: 2720
Reputation: 11
If someone is using IIS 7.5 with Windows Server 2008 R2 follow the code snippet below...
The code works cross browser and handles "Network Failed error" for chrome browser > 51 update
// CSV Generic List
CSVExportGeneric<BookFxDownload> _csv = new CSVExportGeneric<BookFxDownload>(Download);
// Convert to byte array
byte[] a = _csv.ExportToBytes().ToArray();
// the lines are supposed to be in the same order
Response.Clear();
Response.Buffer = true;
Response.ClearHeaders();
Response.ClearContent();
//Response.AppendHeader("content-disposition", fileName);
Response.ContentType = "application/csv";
Response.AddHeader("Content-Length", a.Length.ToString());
//fileName = <yourname>.<extension>
Response.AppendHeader("content-disposition", "attachment; filename=" +"\"" + fileName + "\"");
Response.Charset = "";
Response.OutputStream.Write(a, 0, a.Length);
Response.Flush();
Response.Close();
Upvotes: 0
Reputation: 15041
This threw me for a real loop, but it turned out that only PDF files weren't being downloaded correctly, and that was because of the Content-Type
being set. I probably could have refactored my code differently, but I liked coding it this way best because it was easiest for me to understand and debug. Here's my fixed method:
private void downloadByteStreamAsFile(Byte[] bytes, String fileName)
{
fileName = fileName.Replace(" ", "_");
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.Clear();
if( fileName.Contains(".pdf")){
fileName = HttpUtility.UrlEncode(fileName);
//response.Flush(); //comment this or else no file returned
response.AddHeader("Content-Type", "application/pdf");
response.AddHeader("Content-Disposition",
"attachment; filename=" + fileName + "; size=" + bytes.Length.ToString());
response.BinaryWrite(bytes);
response.Flush();
response.End();
} else {
//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();
}
}
Upvotes: 2
Reputation: 10585
Try putting the filename in quotes.
response.AddHeader(
"Content-Disposition",
"attachment; filename=\"" + fileName + "\"; size=" + bytes.Length.ToString());`
If that works, then see if this still works for IE and Chrome and Safari. If not, add an if
statement to conditionally add the quotes.
Upvotes: 2