Reputation: 19
Below is the code. What are the changes we need to do.
HttpContext context = HttpContext.Current;
context.Response.Clear();
context.Response.AppendHeader("content-disposition", "attachment;filename=" + strFileName + ".docx");
context.Response.Charset = "";
context.Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
var stringWriter = new StringWriter();
stringWriter.Write(strContent);
var htmlWriter = new HtmlTextWriter(stringWriter);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(stringWriter);
HttpContext.Current.Response.Write(oHtmlTextWriter);
context.Response.Write(stringWriter.ToString());
context.Response.End();
Upvotes: 0
Views: 5251
Reputation: 176169
You can't simply write out HTML, give it a .docx file name extension and expect Word to correctly open that document. A .docx file needs to be a valid Office Open XML package, which basically is a zip container with various XML files in it (you can easily see that by renaming a .docx document to .zip and then open it using a standard zip tool).
What you can do now to fix the problem and get a valid Word file:
altChunk
. This is described in another answer here.Upvotes: 1