我正在尝试将 Word 文档(在 ASP .NET 服务器和 Microsoft.Office.Interop.Word 中生成)发送到客户端,但每次我尝试使用 IE9 在客户端打开时,它都说无法打开它因为它已损坏,但如果我尝试保存并打开它,它可以正常工作。那么问题出在哪里?
这是代码:
string nombreDoc = @"C:\tempDocs\Test.doc";
generateIndex(nombreDoc); //this is an internal operation using Interop.Word
FileStream sourceFile = new FileStream(nombreDoc, FileMode.Open);
float FileSize;
FileSize = sourceFile.Length;
byte[] getContent = new byte[(int)FileSize];
sourceFile.Read(getContent, 0, (int)sourceFile.Length);
sourceFile.Close();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Cache.SetNoServerCaching();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ContentType = "application/vnd.ms-word.document";
HttpContext.Current.Response.AddHeader("Content-Length", getContent.Length.ToString());
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=Test.doc");
HttpContext.Current.Response.BinaryWrite(getContent);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
File.Delete(nombreDoc);
我尝试使用 WriteFile 而不是 BynaryWrite 并且问题仍然存在。