繁体   English   中英

DotNetZip 使用 ASP.NET 创建的 Zip 文件有时会导致网络错误

[英]Zip files created by DotNetZip using ASP.NET sometimes causing network error

我正在调试一个涉及 DotNetZip 和 ASP.NET 的奇怪情况。 长话短说,Firefox 正在可靠地下载由代码创建的生成的 zip 文件,但大多数其他浏览器间歇性地返回网络错误。 我已经检查了代码,它的内容与涉及 DotNetZip 的任何内容一样通用。

有什么线索吗?

谢谢!

编辑:这是完整的方法。 正如我所提到的,它几乎是通用的:

protected void btnDownloadFolders_Click(object sender, EventArgs e)
{
    //Current File path
    var diRoot = new DirectoryInfo(_currentDirectoryPath);
    var allFiles = Directory.GetFiles(diRoot.FullName, "*.*", SearchOption.AllDirectories);
    Response.Clear();
    Response.BufferOutput = false;

    var archiveName = String.Format("{0}-{1}.zip", diRoot.Name, DateTime.Now.ToString("yyyy-MM-dd HHmmss"));
    Response.ContentType = "application/zip";
    Response.AddHeader("content-disposition", "inline; filename=\"" + archiveName + "\"");

    using (var zip = new ZipFile())
    {
        foreach (var strFile in allFiles)
        {
            var strFileName = Path.GetFileName(strFile);
            zip.AddFile(strFile,
                        strFile.Replace("\\" + strFileName, string.Empty).Replace(diRoot.FullName, string.Empty));
        }

        zip.Save(Response.OutputStream);
    }
    Response.Close();
}

这可能是因为您没有发送content-length 我已经看到将文件发送到未指定的浏览器时发生错误。 因此,在MemoryStream创建 zip 文件。 将流保存到字节数组,以便您也可以将长度作为响应发送。 虽然我不能肯定地说它会解决你的具体问题。

byte[] bin;

using (MemoryStream ms = new MemoryStream())
{
    using (var zip = new ZipFile())
    {
        foreach (var strFile in allFiles)
        {
            var strFileName = Path.GetFileName(strFile);
            zip.AddFile(strFile, strFile.Replace("\\" + strFileName, string.Empty).Replace(diRoot.FullName, string.Empty));
        }

        //save the zip into the memorystream
        zip.Save(ms);
    }

    //save the stream into the byte array
    bin = ms.ToArray();
}

//clear the buffer stream
Response.ClearHeaders();
Response.Clear();
Response.Buffer = true;

//set the correct contenttype
Response.ContentType = "application/zip";

//set the filename for the zip file package
Response.AddHeader("content-disposition", "attachment; filename=\"" + archiveName + "\"");

//set the correct length of the data being send
Response.AddHeader("content-length", bin.Length.ToString());

//send the byte array to the browser
Response.OutputStream.Write(bin, 0, bin.Length);

//cleanup
Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM