2

我想从 asp.net 在 IE 中打开 docx 文件。IIS 具有正确映射的 mime 类型。我可以很好地打开 pdf,但 docx 总是会提示我下载,例如 content-disposition='attachment'。有没有什么设置可以做?

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

3 回答 3

1

被测试的计算机上是否安装了Microsoft Word 或Word Viewer

如果没有处理应用程序,浏览器将别无选择,只能下载文件。

如果安装了 Word,您可能还想检查您的 Windows 文件类型是否将 .docx 映射到 Word、另一个应用程序或什么都没有。使用此MSKB 文章中的说明进行检查

于 2010-10-09T22:29:11.977 回答
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()

只需填写空白即可。我想你可能只是有点太多了。

更新:

我相信您可能已经看过这些网站,但我会将它们放在这里以供其他人偶然发现:

从 IE 下载 Docx - 在 IIS 中设置 MIME 类型 http://blogs.msdn.com/b/vsofficedeveloper/archive/2008/05/08/office-2007-open-xml-mime-types.aspx

我不确定您的方法到底在哪里失败。如果我有更多时间,我会自己尝试一下;也许今晚晚些时候。现在祝你好运!

于 2010-07-02T02:53:49.083 回答
0

SSL 下的旧版本 IE(IE-8 之前)存在此问题。IIS 7.5+ 的服务器端修复是使用URL 重写扩展添加出站规则以去除 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>
于 2014-02-12T17:38:09.723 回答