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
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