Opening Office 2010 documents from C# error

I am trying to load office documents for a user by

HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name); 
HttpContext.Current.Response.ContentType = MIMETypesDictionary[fileExt];
HttpContext.Current.Response.BinaryWrite(fileArray);

and my MIMETypesDictionary has this (abbreviated)

    private static readonly Dictionary<string, string> MIMETypesDictionary = new Dictionary<string, string>
      {
        {"doc", "application/msword"},
        {"docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
        ...
        {"xls", "application/vnd.ms-excel"},  
        {"xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}
        ...
      };

doc and xls files work great. Docx and xlsx files error in the client application.

Excel errors

Excel found unreadable content in 'filename.xlsx'. Do you want to recover the contents of this workfbook?

Word errors

The file filename.docx cannot be opened because there are problems with the contents.

if I click Yes, it loads and the docs looks okay. This error won't fly for users of course. Am I using the right mime types for these files? I also tried application/vnd.ms-word.document.12, application/vnd.ms-excel.12 and application/octet-stream and got the same errors.

Thanks!


resolved! had to build out the response code

HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name); 
HttpContext.Current.Response.ContentType = MIMETypesDictionary[fileExt];
HttpContext.Current.Response.BinaryWrite(fileArray);
HttpContext.Current.Response.End();
HttpContext.Current.Response.Flush();

and i kept the octet-stream values for the file types and it works great!

链接地址: http://www.djcxy.com/p/45480.html

上一篇: Fileinfo在php版本5.4.22和5.3.3上返回不同的mimetype

下一篇: 从C#错误中打开Office 2010文档