60

I have a section on a website where I display a pdf inside a light box. The recent chrome upgrade has broken this displaying:

Error 349 (net::ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION): Multiple Content-Disposition headers received. This is disallowed to protect against HTTP response-splitting attacks.

This still works correctly in IE.

I'm using ASP.NET MVC3 on IIS6

The code I use to generate the file is as follows.

If I remove the inline statement then the file downloads, however that breaks the lightbox functionality.

Problem Code

public FileResult PrintServices()
{
    //... unrelated code removed
    MemoryStream memoryStream = new MemoryStream();
    pdfRenderer.PdfDocument.Save(memoryStream);
    string filename = "ServicesSummary.pdf";

    Response.AppendHeader("Content-Disposition", "inline;");

    return File(memoryStream.ToArray(), "application/pdf", filename);
}

The Fix

Remove

Response.AppendHeader("Content-Disposition", "inline;");

Then Change

return File(memoryStream.ToArray(), "application/pdf", filename);

to

return File(memoryStream.ToArray(), "application/pdf");
Jim G.
  • 14,056
  • 19
  • 94
  • 153
BlairM
  • 738
  • 1
  • 5
  • 12
  • Related: http://stackoverflow.com/questions/1187261/whats-the-difference-between-the-four-file-results-in-asp-net-mvc – Jim G. Jan 11 '16 at 16:24
  • Possible duplicate of [Returning a file to View/Download in ASP.NET MVC](http://stackoverflow.com/questions/5826649/returning-a-file-to-view-download-in-asp-net-mvc) – Jim G. Jan 11 '16 at 19:23
  • Related: http://stackoverflow.com/a/16673120/109941 – Jim G. Jan 11 '16 at 19:30
  • 1
    Just to ask is there any comma in file name, because I just have that issue. If so, remove any commas from file name if that is an option. – 1392023093user Apr 08 '16 at 00:32

7 Answers7

77

The solution above is fine if you don't need to specify the filename, but we wanted to keep the filename default specified for the user.

Our solution ended up being the filename itself as it contained some commas. I did a replace on the commas with "" and the file now delivers the document as expected in Chrome.

FileName = FileName.Replace(",", "")

Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment; filename=" & FileName)    
Response.BinaryWrite(myPDF)
2GDave
  • 996
  • 8
  • 18
  • 3
    You can also add inverted commas around the filename. – Nik Mar 25 '12 at 14:47
  • Same issue and solution with DocRaptor and PHP `header()`. Thanks. – Boaz Jan 07 '13 at 08:59
  • 39
    Same issue here, but only fixed by putting the filename in quotes: `Response.AddHeader("content-disposition", "attachment; filename=\"" + FileName + "\"")` – roryok Apr 03 '13 at 09:46
  • 2
    @Nik you should post an answer with the Quotation Marks solution becouse is completely different from this one and works very well. I would upvote that answer. – daniloquio Apr 05 '13 at 18:14
  • Had this issue today with Azure Blob Storage and Content-Disposition header. Thanks for this fix - works great! – Ian Schmitz Aug 26 '14 at 22:03
  • Removing the comma works, but if you quote the filename then you can keep the comma. See comment by @roryok above and better solutions without as many upvotes. – Gerry Gurevich Sep 23 '14 at 10:23
20

I used @roryok's comment, wrapping the filename in quotes:

Response.AddHeader("content-disposition", "attachment; filename=\"" + FileName + "\"")

@a coder's answer of using single quotes did not work as expected in IE. The file downloaded with the single quotes still in the name.

Homer
  • 7,084
  • 13
  • 66
  • 104
  • This solution worked for me. I just added in the escaped double quotes and the problem resolved itself. Thanks! – IAmTimCorey Nov 14 '16 at 21:47
  • Hay, I tried this, but the download happens with an underscore prefixed and suffixed to the file name. (Content-Disposition: attachment; filename="ab,cd.pptx") Actual name -> ab,cd.pptx... NameOnDownload -> _ab,cd.pptx_ ... What can I do for this issue? – SSB Mar 29 '21 at 05:39
2

Had this problem today. Per roryok and others, the solution was to put the filename in quotes.

Previous, Chrome FAIL:

header("Content-Disposition: attachment; filename=$file");

Current, Chrome OK:

header("Content-Disposition: attachment; filename='$file'");

Note the quotes around $file.

a coder
  • 6,796
  • 19
  • 77
  • 121
  • For me in IE, single quotes caused the file to download with single quotes around the filename: 'Blah, Inc. 847.pdf' – Homer Aug 14 '14 at 16:39
2

I was having the same issue and fixed it by just removing the file name from the return statement.

Change:

 return File(outStream.ToArray(), "application/pdf", "Certificate.pdf");

to:

 return File(outStream.ToArray(), "application/pdf");

And KEPT the:

Response.AddHeader("content-disposition", "attachment;filename=\"" + "Certificate.pdf" + "\"");

This still keeps the name for the downloaded file.

Barry Franklin
  • 1,593
  • 1
  • 26
  • 42
0

My issue was due to the double quote as shown below:

var encoding = System.Text.Encoding.UTF8;

*Response.AddHeader("Content-Disposition", string.Format("attachment; filename=**\"{0}\"**", HttpUtility.UrlEncode(file, encoding)));*

Changing the above to this worked!

*Response.AddHeader("Content-Disposition", string.Format("attachment; filename=**{0}**", HttpUtility.UrlEncode(file, encoding)));*
Phil Rykoff
  • 11,403
  • 2
  • 36
  • 62
hatsrumandcode
  • 1,821
  • 2
  • 19
  • 19
0

to fix this for any file type with a custom file name remove the (or similar headers)

Response.AppendHeader("Content-Disposition", "inline;");

and add

string fileName = "myfile.xlsx"
return File(fileStream, System.Web.MimeMapping.GetMimeMapping(Path.GetFileName(filePath)), fileName);

you can also use the filepath instead of a stream in the first parameter

tedbaker
  • 23
  • 7
0

This solution will preserve filename AND open file in browser (.net mvc)

Response.Headers["Content-Disposition"] = "inline;filename=\"" + theFile.filename + "\"";
return File(filePath, mimeType);//don't specify filename. It will create second Content-Disposition header
shlasasha
  • 133
  • 1
  • 10