132

Duplicate headers received from server

The response from the server contained duplicate headers. This problem is generally the result of a misconfigured website or proxy. Only the website or proxy administrator can fix this issue.

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

I found this error while exporting to pdf in chrome.

Response.Buffer = false;
Response.ClearHeaders();
string ext = objProp.PACKAGEFILENAME.Substring(objProp.PACKAGEFILENAME.LastIndexOf("."));
string ext1 = ext.Substring(1);
Response.ContentType = ext1;
Response.AddHeader("Content-Disposition", "target;_blank,attachment; filename=" + objProp.PACKAGEFILENAME);
const int ChunkSize = 1024;
byte[] binary = objProp.PACKAGEDOCUMENT;
System.IO.MemoryStream ms = new System.IO.MemoryStream(binary);
int SizeToWrite = ChunkSize;

for (int i = 0; i < binary.GetUpperBound(0) - 1; i = i + ChunkSize)
{
    if (!Response.IsClientConnected) return;
    if (i + ChunkSize >= binary.Length) SizeToWrite = binary.Length - i;
    byte[] chunk = new byte[SizeToWrite];
    ms.Read(chunk, 0, SizeToWrite);
    Response.BinaryWrite(chunk);
    Response.Flush();
}
Response.Close();

How to fix this?

Community
  • 1
  • 1
Purvesh Desai
  • 1,797
  • 2
  • 15
  • 39
  • 20
    I have also found that comma in the filename will give that error (in Chrome only). I am thinking there must be a way to tell it that the filename="abc,xyz.pdf" is valid. I get that we can replace the "," with something else, but I want to preserve and return the filename exactly as it is. None of the other browsers (IE, FireFox, Safari, Opera) I have tried have this issue. – cusman Feb 10 '14 at 21:08
  • 33
    So there is a way to let it still have commas in the filename. Just have to quote the filename. `Response.AddHeader("content-disposition", "attachment; filename=\"" + FileNameWithCommas + "\"");` – cusman Feb 10 '14 at 21:15
  • 4
    Thanks @cusman. That was exactly my problem - a comma in the filename AND only Chrome had an issue with that. – PeterKA Mar 31 '15 at 18:46
  • 1
    Possible duplicate of [Chrome, pdf display, Duplicate headers received from the server](http://stackoverflow.com/questions/8588818/chrome-pdf-display-duplicate-headers-received-from-the-server) – Ian Kemp Jun 10 '16 at 09:27

6 Answers6

253

This ones a little old but was high in the google ranking so I thought I would throw in the answer I found from Chrome, pdf display, Duplicate headers received from the server

Basically my problem also was that the filename contained commas. Do a replace on commas to remove them and you should be fine. My function to make a valid filename is below.

    public static string MakeValidFileName(string name)
    {
        string invalidChars = Regex.Escape(new string(System.IO.Path.GetInvalidFileNameChars()));
        string invalidReStr = string.Format(@"[{0}]+", invalidChars);
        string replace = Regex.Replace(name, invalidReStr, "_").Replace(";", "").Replace(",", "");
        return replace;
    }
Community
  • 1
  • 1
Bryan Roberts
  • 3,449
  • 1
  • 18
  • 22
  • 25
    From the comment of @roryok at the linked answer, quotes around the filename should fix the issues. And actually [the spec](http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.5.1) seems to say that filename should be quoted. – Touko Nov 04 '13 at 13:16
  • The quotation marks would be a help (and should be added) but this function has the added benefit of stripping out non valid characters so you make a friendlier name at the end. – Bryan Roberts Nov 08 '13 at 19:09
  • @Younisbarznji it worked for me, you need to escape the quotes in the filename – coffekid Dec 24 '15 at 00:49
  • 3
    It works for me! Commas and spaces are not good idea in filenames! – Hugo Feb 02 '17 at 08:52
  • Why `.Replace(";", "").Replace(",", "")` when already replacing by RegEx? Add `;,` to your RegEx pattern: `string invalidReStr = string.Format(@"[{0};,]+", invalidChars); string replace = Regex.Replace(name, invalidReStr, "_");` – SBF Oct 23 '19 at 14:05
106

The server SHOULD put double quotes around the filename, as mentioned by @cusman and @Touko in their replies.

For example:

Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
tomc
  • 1,458
  • 1
  • 10
  • 16
6

Just put a pair of double quotes around your file name like this:

this.Response.AddHeader("Content-disposition", $"attachment; filename=\"{outputFileName}\"");

user3578181
  • 1,896
  • 2
  • 17
  • 12
6

For me the issue was about a comma not in the filename but as below: -

Response.ok(streamingOutput,MediaType.APPLICATION_OCTET_STREAM_TYPE).header("content-disposition", "attachment, filename=your_file_name").build();

I accidentally put a comma after attachment. Got it resolved by replacing comma with a semicolon.

Deepak Agrawal
  • 121
  • 1
  • 4
3

Double quotes around the filename in the header is the standard per MDN web docs. Omitting the quotes creates multiple opportunities for problems arising from characters in the filename.

0

I've got this same error (ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION) downloading from EDGE a dynamic filename named

"a,b,c test.zip"

The problem was the comma.

Strangely, using the old "internet explorer", the comma was not a problem and the same file was downloaded (same procedure same file). Using other browser the error occured.

WORKAROUND: normalize filename (by code) replacing any comma into underscore, if evolved browsers are used.