views:

197

answers:

2

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();
A: 

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:

http://stackoverflow.com/questions/179315/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!

Cory Larson
Thanks for the response. My issue is that docx doesnt open inline in browser. It always works with "attachment". I tried your code above by changing content-disposition to inline but it still opened as attachment. If i use the code for content-type = "applicaiton/pdf" for pdf files than it works fine. I think it is mime type issue with browser but i am unable to figure it out. It has same behaviour in all browsers.
mac73
A: 

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

John K