我正在尝试通过以下方式为用户加载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.如果我单击Yes,它将加载,并且文档看起来没有问题。当然,这个错误对用户来说是不可能的。我是否对这些文件使用了正确的mime类型?我还尝试了application/vnd.ms-word.document.12、application/vnd.ms-excel.12和application/octet-stream,得到了相同的错误。
谢谢!
发布于 2012-01-17 01:24:49
解决了!必须构建响应代码
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();我保留了文件类型的八位位流的值,它工作得很好!
https://stackoverflow.com/questions/8883524
复制相似问题