I am using Response to have my application open up a Word Document for the user. If the user chooses to save the file, it saves it and the file looks how it should when you open it. If the user chooses to open the file right away, they receive an error saying that IE couldn't open the file. If they choose 'Retry', MS Word shows an error saying it cannot find the file. Below are screen shots showing my situation. Also, here is the code I have to display the file:
this.Context.Response.Clear();
this.Context.Response.ClearContent();
this.Context.Response.ClearHeaders();
this.Context.Response.BufferOutput = true;
this.Context.Response.ContentType = "application/msword";
this.Context.Response.AppendHeader("Content-Length", bytes.Length.ToString());
this.Context.Response.AddHeader("Content-Disposition", "attachment; filename=" + "Test Document.doc");
this.Context.Response.BinaryWrite(bytes);
this.Context.ApplicationInstance.CompleteRequest();
Here is the screen when prompting the user of the download:
Here is the screen after user chooses 'Open'
Here is the screen after user chooses 'Retry'. This screen is coming for MS Word.
****EDIT**** I have found a bit of code online that I tried testing and issue still occures when I call this function:
protected void GenerateMsWordDoc()
{
string strBody = "<html>" +
"<body>" +
"<div>Your name is: <b>Billy Bob</b></div>" +
"<table width='100%' style='background-color:#cfcfcf;'><tr><td>1st Cell body data</td><td>2nd cell body data</td></tr></table>" +
"Ms Word document generated successfully." +
"</body>" +
"</html>";
string fileName = "MsWordSample.doc";
// You can add whatever you want to add as the HTML and it will be generated as Ms Word docs
Response.AppendHeader("Content-Type", "application/msword");
Response.AppendHeader ("Content-disposition", "attachment; filename="+ fileName);
Response.Write(strBody);
}
Spaces in the filename parameter of content-disposition have been known to cause errors across different browser versions. Try enclosing the filename in double quotes:
this.Context.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + "Test Document.doc" + "\"");
Can you post the sample data being used? I tried below code in IE9 works fine.
this.Context.Response.Clear();
this.Context.Response.ClearContent();
this.Context.Response.ClearHeaders();
this.Context.Response.BufferOutput = true;
this.Context.Response.ContentType = "application/msword";
this.Context.Response.AppendHeader("Content-Length", "12");
this.Context.Response.AddHeader("Content-Disposition", "attachment; filename=" + "Test Document.doc");
this.Context.Response.BinaryWrite(new byte[] { });
this.Context.ApplicationInstance.CompleteRequest();
Your recent code is also working fine. I am using IE9. Below are version details...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With