簡體   English   中英

從C#錯誤打開Office 2010文檔

[英]Opening Office 2010 documents from C# error

我正在嘗試為用戶加載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();

我保留了文件類型的八位字節流值,效果很好!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM