0

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:

Please help

Update: Code works fine in IE and Chrome, only Firefox has this issue of losing the file extension

Community
  • 1
  • 1
rlb.usa
  • 14,942
  • 16
  • 80
  • 128
  • try to use `response.AddHeader("Content-Type", "application/pdf");` – Iłya Bursov Aug 02 '15 at 04:35
  • @Lashane : ( Nope, doesn't help. But I did figure out it is firefox only – rlb.usa Aug 02 '15 at 04:41
  • possible duplicate of [Why Does FireFox Not Include the .xml Extension when Downloading a File?](http://stackoverflow.com/questions/1120599/why-does-firefox-not-include-the-xml-extension-when-downloading-a-file) – rlb.usa Aug 02 '15 at 04:47
  • Here is the answer http://stackoverflow.com/questions/1120599/why-does-firefox-not-include-the-xml-extension-when-downloading-a-file – rlb.usa Aug 02 '15 at 04:47

3 Answers3

2

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.

tacos_tacos_tacos
  • 10,277
  • 11
  • 73
  • 126
2

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();

        }
    }
rlb.usa
  • 14,942
  • 16
  • 80
  • 128
0

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();