0

I'm getting ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION when returning my docx file, however I'm not sure where in my header the error comes from. I've tried my best to remove anything that is dynamic and hardcoded to figure it out, to no avail.

byte[] byteArray = File.ReadAllBytes(@"T:\Praktik log\Learning Goals.docx");
            using (MemoryStream mem = new MemoryStream())
            {
                mem.Write(byteArray,0,byteArray.Length);
                using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(mem, true))
                {
                    RevisionAccepter.AcceptRevisions(wordDoc);
                    wordDoc.MainDocumentPart.Document.Body.InsertAt(
                        new Paragraph(
                            new Run(
                                new Text("Newly inserted paragraph."))), 0);
                }


                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.Cookies.Clear();
                //Add the header & other information      
                Response.Cache.SetCacheability(HttpCacheability.Private);
                Response.CacheControl = "private";
                Response.Charset = System.Text.UTF8Encoding.UTF8.WebName;
                Response.ContentEncoding = System.Text.UTF8Encoding.UTF8;
                Response.AppendHeader("Content-Length", mem.ToArray().Length.ToString());
                Response.AppendHeader("Pragma", "cache");
                Response.AppendHeader("Expires", "60");
                Response.AppendHeader("Content-Disposition",
                    "attachment; " +
                    "filename=\"ActivationKey.docx\"; " +
                    "size=" + mem.ToArray().Length + "; " +
                    "creation-date=" + DateTime.Now.ToString("R") + "; " +
                    "modification-date=" + DateTime.Now.ToString("R") + "; " +
                    "read-date=" + DateTime.Now.ToString("R"));
                Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                //Write it back to the client    
                Response.BinaryWrite(mem.ToArray());
                Response.End();

I can download it on IE as it's gives crap about security, but chrome says nogo, and I need to have Chrome support for this download.

EDIT: Fiddler response: https://prnt.sc/pddibg

Meik F
  • 61
  • 1
  • 10

1 Answers1

1

You're doing one weird thing.

Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.Cookies.Clear();

ASP.NET places some headers itself.
Please try to not remove them and use native (if they exist for headers you're trying to modify, of course) ASP.NET methods to modify response headers.

Also you can setup a HTTP debugging proxy.
It will help you see what exactly you are doing wrong.
Here's some HTTP debugging proxies I know: Fiddler, Wireshark or Charles (paid, requires Java).

This guy achieved file downloading via returning System.IO.File.
Also this question looks like yours.

Check these answers, they may help you.

I achieved downloading a named file via:

[HttpGet]
public void Get()
{
    Response.Headers.Add("Content-Disposition", "attachment; filename=\"file.bin\"");
    var buffer = Enumerable.Repeat((byte) 0xF, 2048).ToArray(); //bytes
    Response.Body.Write(buffer, 0, buffer.Length);
}

Tested on Google Chrome (Version 77.0.3865.90).
File downloaded and contains 2048 bytes of data.

Server response in Fiddler: screenshot

Vyacheslav
  • 559
  • 2
  • 11
  • Remove those things make no difference. Also, the first guy is using what looks to be MVC or web API, returning a result. I'm editing a file in memory and converting those bytes to the receiver, which doesn't look like what he is doing. The second guy mentions filename, which isn't an issue here from what I see. This is my response from fiddler; which responds with 200. https://prnt.sc/pddibg – Meik F Oct 01 '19 at 12:51
  • Updated my answer, @MeikF please check it. – Vyacheslav Oct 01 '19 at 13:30
  • 1
    If nothing works - please minimize your Content-Disposition header. Leave only attachment and filename. – Vyacheslav Oct 01 '19 at 13:48
  • Turns out it is something in the 2nd part of my disposition header, with the dates. – Meik F Oct 03 '19 at 07:37
  • Glad you figured it out! – Vyacheslav Oct 03 '19 at 10:07