问题
I want to open docx file in IE from asp.net. The IIS has mime type correctly mapped. I can open pdf fine but docx will always prompt me to download like content-disposition='attachment'. Is there any setting to be done?
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();
回答1:
Is Microsoft Word or Word Viewer installed on the computer being tested with?
If the handling application is absent, the browser won't have much choice but to download the file.
If Word is installed, you might also want to check if your Windows File Types have .docx mapped to Word, to another app or to nothing. Check by using instructions in this MSKB article
回答2:
I generally follow this format for forcing files at users: It's given me the best results in all browsers (forgive the VB instead of 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()
Just fill in the blanks. I think you might just have a little too much going on.
UPDATE:
I'm sure you've probably already seen these sites, but I'll put them here for others to stumble-upon:
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
I'm not sure exactly where your method is failing. If I had more time I would try it out myself; maybe later tonight. Good luck for now!
回答3:
This problem exists with older version of IE (pre IE-8) under SSL. The server-side fix with IIS 7.5+ is to use the URL Rewrite extention to add an outbound rule to strip off the "no-store" value in the Cache-Control header, and to strip the Pragma header. This rule set would do the trick:
<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>
来源:https://stackoverflow.com/questions/3162989/docx-file-doesnt-open-in-browser-with-content-disposition-inline-in-ie-8