我想从asp.net在IE中打开docx文件。 IIS具有正确映射的mime类型。我可以打开pdf,但docx总是会提示我下载内容 - 配置='附件'。有什么设置要做吗?
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.Cookies.Clear();
Response.Cache.SetCacheability(HttpCacheability.Private);
Response.CacheControl = "private";
Response.Charset = System.Text.UTF8Encoding.UTF8.WebName;
Response.ContentEncoding = System.Text.UTF8Encoding.UTF8;
Response.AppendHeader("Content-Length", buffer.Length.ToString());
Response.AppendHeader("Pragma", "cache");
Response.AppendHeader("Expires", "60");
Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
Response.AppendHeader("Content-Disposition",
"inline; " +
"filename=\"" + "test.docx" + "\"; " +
"size=" + buffer.Length.ToString() + "; " +
"creation-date=" + DateTime.Now.ToString("R") + "; " +
"modification-date=" + DateTime.Now.ToString("R") + "; " +
"read-date=" + DateTime.Now.ToString("R"));
Response.BinaryWrite(buffer);
Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();
Response.End();
答案 0 :(得分:1)
正在测试的计算机上安装了Microsoft Word或Word Viewer吗?
如果没有处理申请,浏览器将没有多少选择,只能下载文件。
如果安装了Word,您可能还需要检查Windows文件类型是否将.docx映射到Word,另一个应用程序或任何内容。请使用此MSKB article
中的说明进行检查答案 1 :(得分:0)
我通常遵循这种格式强制用户使用文件:它在所有浏览器中都给了我最好的结果(原谅VB而不是C#):
Response.Clear()
Response.ClearHeaders()
Response.Buffer = True
Response.ContentType = "your mime type"
Response.CacheControl = "public"
Response.AddHeader("Pragma", "public")
Response.AddHeader("Expires", "0")
Response.AddHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0")
Response.AddHeader("Content-Description", "Description of your content")
Response.AddHeader("Content-Disposition", "attachment; filename=""somefile.pdf""")
Response.BinaryWrite(buffer)
Response.Flush()
Response.End()
填写空白。我想你可能只是有点太多了。
<强>更新强>
我相信你可能已经看过这些网站了,但我会把它们放在这里让其他人偶然发现:
Downloading Docx from IE - Setting MIME Types in IIS http://blogs.msdn.com/b/vsofficedeveloper/archive/2008/05/08/office-2007-open-xml-mime-types.aspx
我不确定你的方法究竟在哪里失败。如果我有更多的时间,我会自己尝试一下;也许晚些时候。祝你好运!
答案 2 :(得分:0)
SSL下的旧版IE(IE-8之前版本)存在此问题。 IIS 7.5+的服务器端修复是使用URL Rewrite extention添加出站规则以去除Cache-Control标头中的“no-store”值,并剥离Pragma标头。这个规则集可以解决问题:
<outboundRules>
<rule name="Always Remove Pragma Header">
<match serverVariable="RESPONSE_Pragma" pattern="(.*)" />
<action type="Rewrite" value="" />
</rule>
<rule name="Remove No-Store for Attachments">
<conditions>
<add input="{RESPONSE_Content-Disposition}" pattern="attachment" />
</conditions>
<match serverVariable="RESPONSE_Cache-Control" pattern="no-store" />
<action type="Rewrite" value="max-age=0" />
</rule>
</outboundRules>