从C#错误中打开Office 2010文档
我正在尝试为用户加载Office文档
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
HttpContext.Current.Response.ContentType = MIMETypesDictionary[fileExt];
HttpContext.Current.Response.BinaryWrite(fileArray);
和我的MIMETypesDictionary有这个(缩写)
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和xls文件很好用。 Docx和xlsx文件在客户端应用程序中出错。
Excel错误
Excel found unreadable content in 'filename.xlsx'. Do you want to recover the contents of this workfbook?
字错误
The file filename.docx cannot be opened because there are problems with the contents.
如果我点击是,它会加载并且文档看起来不错。 当然这个错误不会为用户提供。 我是否在这些文件中使用了正确的MIME类型? 我也试过application / vnd.ms-word.document.12,application / vnd.ms-excel.12和application / octet-stream,并且得到了相同的错误。
谢谢!
解决! 必须构建响应代码
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();
并且我保留了文件类型的八位字节流值,而且效果很好!
链接地址: http://www.djcxy.com/p/45479.html