0

I am using following code to output html string into word document with .docx extension [not .doc]

private void ExportBodyAsDoc(string strBody) {
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.ContentType = "Application/msword";
    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=myfile.docx");
    var repo = new System.IO.MemoryStream();
    var stringBytes = System.Text.Encoding.UTF8.GetBytes(strBody);
    repo.Write(stringBytes, 0, strBody.Length);
    HttpContext.Current.Response.BinaryWrite(repo.ToArray());
    HttpContext.Current.Response.Flush();
    HttpContext.Current.Response.Close();
    HttpContext.Current.Response.End();
}

It is only working with firefox while in other it is corrupting the document.

Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
Saad
  • 1,312
  • 5
  • 17
  • 40

1 Answers1

0

Perhaps you need to set the content encoding of the output.

Seeing as you're using UTF8, the following should work:

HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;

http://msdn.microsoft.com/en-us/library/system.web.httpresponse.contentencoding.aspx http://msdn.microsoft.com/en-us/library/system.text.encoding.utf8.aspx

Sean Airey
  • 6,352
  • 1
  • 20
  • 38