asp.net docx文件无法在浏览器中打开,内容处置内嵌在IE 8中

pgvzfuti  于 2022-11-26  发布在  .NET
关注(0)|答案(3)|浏览(156)

我想从ASP.NET在IE中打开docx文件。IIS已经正确Map了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();
vjrehmav

vjrehmav1#

正在测试的计算机上是否安装了Microsoft Word或Word Viewer
如果处理应用程序不存在,浏览器将没有太多选择,只能下载文件。
如果已安装Word,您可能还需要检查您的Windows文件类型是否将.docxMap到Word、其他应用程序或未Map。请使用此MSKB article中的说明进行检查

icnyk63a

icnyk63a2#

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

5fjcxozz3#

我通常遵循以下格式在用户处强制文件:它在所有浏览器中给了我最好的结果(原谅用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 IIShttp://blogs.msdn.com/b/vsofficedeveloper/archive/2008/05/08/office-2007-open-xml-mime-types.aspx显示器
我不知道你的方法到底哪里不对。如果我有更多的时间,我会自己试试;也许今晚晚些时候祝你好运!

相关问题