为什么从ASP.NET页下载时的.docx文件被损坏? [英] Why are .docx files being corrupted when downloading from an ASP.NET page?

查看:696
本文介绍了为什么从ASP.NET页下载时的.docx文件被损坏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个以下code为使页附件用户:

I have this following code for bringing page attachments to the user:

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,以便固定被打开。

The problem is that all supported files works properly (jpg, gif, png, pdf, doc, etc), but .docx files, when downloaded, are corrupted and they need to be fixed by Office in order to be opened.

起初我不知道这个问题是在uncom pressing包含了.DOCX的zip文件,这样反而把输出文件只在响应,我先救了它,并且文件打开成功,所以我知道这个问题应该在应对写作。

At first I didn't know if the problem was at uncompressing the zip file that contained the .docx, so instead of putting the output file only in the response, I saved it first, and the file opened successfully, so I know the problem should be at response writing.

你知道什么能发生?

推荐答案

我也遇到了这个问题,居然找到了答案在这里:<一href=\"http://www.aspmessageboard.com/showthread.php?t=230778\">http://www.aspmessageboard.com/showthread.php?t=230778

I also ran into this problem and actually found the answer here: http://www.aspmessageboard.com/showthread.php?t=230778

原来的DOCX格式需要有到Response.End()的Response.BinaryWrite之后。

It turns out that the docx format needs to have Response.End() right after the Response.BinaryWrite.

这篇关于为什么从ASP.NET页下载时的.docx文件被损坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆