Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 输出word文档作为docx扩展,仅在firefox中工作_Asp.net_Ms Word_Httpcontext - Fatal编程技术网

Asp.net 输出word文档作为docx扩展,仅在firefox中工作

Asp.net 输出word文档作为docx扩展,仅在firefox中工作,asp.net,ms-word,httpcontext,Asp.net,Ms Word,Httpcontext,我正在使用以下代码将html字符串输出到扩展名为.docx[not.doc]的word文档中 private void ExportBodyAsDoc(string strBody) { HttpContext.Current.Response.Clear(); HttpContext.Current.Response.ContentType = "Application/msword"; HttpContext.Current.Response.AddHeader("C

我正在使用以下代码将html字符串输出到扩展名为.docx[not.doc]的word文档中

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();
}

它只与firefox一起工作,而在另一种情况下,它会破坏文档。

也许您需要设置输出的内容编码

鉴于您正在使用UTF8,以下各项应该可以工作:

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