【发布时间】:2011-10-19 15:31:26
【问题描述】:
我正在生成一个 *.docx 文件服务器端,我想在按下按钮时返回给用户。在按钮的事件处理程序中,我目前将文档数据生成为byte[](在以下代码示例中称为bytes),并将其返回给用户,如下所示:
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=Test.docx;");
Response.AddHeader("Content-Length", bytes.Length.ToString(CultureInfo.InvariantCulture));
Response.ContentType = "application/vnd.ms-word.document.12";
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.BinaryWrite(bytes);
Response.Flush();
Response.Close();
这会导致 IE 出现奇怪的行为,即当用户在下载时单击“打开”按钮时,word 2010 会启动并指出“Word 在尝试打开文件时遇到错误”。但是,如果用户选择先将文件保存在某处,word 会很好地打开保存的文档。
我尝试了不同的内容类型、缓存和编码选项,但无济于事。任何线索可能导致这种行为?
【问题讨论】: