我正在尝试将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,问题仍然存在。