【问题标题】:docx file doesnt open in browser with content disposition inline in IE 8docx 文件无法在 IE 8 中内联内容处置的浏览器中打开
【发布时间】:2011-03-10 22:20:52
【问题描述】:

我想从 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();

【问题讨论】:

  • ...CompleteRequest() 真的有必要吗?

标签: asp.net content-disposition


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    我通常遵循这种格式来强制用户文件:它在所有浏览器中都给了我最好的结果(原谅 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

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

    【讨论】:

    • 感谢您的回复。我的问题是 docx 没有在浏览器中打开内联。它始终与“附件”一起使用。我通过将 content-disposition 更改为 inline 来尝试上面的代码,但它仍然作为附件打开。如果我对 pdf 文件使用 content-type = "applicaiton/pdf" 的代码,它就可以正常工作。我认为这是浏览器的 mime 类型问题,但我无法弄清楚。它在所有浏览器中都有相同的行为。
    【解决方案3】:

    此问题存在于 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-25
      • 2012-06-16
      • 2021-01-24
      • 2014-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多