简体   繁体   中英

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

I'm debugging a rather odd situation involving DotNetZip and ASP.NET. Long story short, the resulting zip files that are being created by the code are being reliably downloaded by Firefox, but most other browsers are intermittently returning a Network Error. I've examined the code and it reads about as generically as anything that involves DotNetZip.

Any clues?

Thanks!

EDIT: Here's the complete method. As I mentioned, it's about as generic as it gets:

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();
}

It could be because you are not sending the content-length . I've seen errors occur in sending files to the browser where it was not specified. So create the zip file in a MemoryStream . save the stream to a Byte Array so you can send the length as a Response also. Although I can't say for sure that it will fix your specific problem.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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