ページの添付ファイルをユーザーに表示するための次のコードがあります。
private void GetFile(string package, string filename)
{
var stream = new MemoryStream();
try
{
using (ZipFile zip = ZipFile.Read(package))
{
zip[filename].Extract(stream);
}
}
catch (System.Exception ex)
{
throw new Exception("Resources_FileNotFound", ex);
}
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/unknown";
if (filename.EndsWith(".docx"))
{
Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
}
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
Response.BinaryWrite(stream.GetBuffer());
stream.Dispose();
Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
問題は、サポートされているすべてのファイル (jpg、gif、png、pdf、doc など) が正常に機能することですが、.docx ファイルをダウンロードすると破損しており、開くには Office で修正する必要があります。
最初は問題が .docx を含む zip ファイルの解凍にあるのかどうかわからなかったので、出力ファイルのみを応答に入れる代わりに、最初に保存し、ファイルが正常に開かれたので、問題がわかりました。応答の書き込みにある必要があります。
何が起こっているか知っていますか?