-4

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();
}
VDWWD
  • 35,079
  • 22
  • 62
  • 79
  • Please provide a Minimal, Complete, Verifiable example, http://stackoverflow.com/help/mcve – Sunshine Feb 03 '17 at 15:48
  • You need to provide more information than this. We can't see your screen or examine your network traffic. I assume you're here about a code issue? How can we help if we can't see the code? –  Feb 03 '17 at 15:49
  • To solve this problem, change `Response.Close();` to `Response.Flush();` See https://stackoverflow.com/a/736462/481207. (Chrome version 61.) – Matt Nov 12 '17 at 21:20

1 Answers1

1

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();
VDWWD
  • 35,079
  • 22
  • 62
  • 79
  • Sorry for the delay - I'm going to try this solution on our Alpha area and see if it works for us. Thanks! – David Stowell Feb 07 '17 at 20:13
  • For lack of a better place to put the results, this code works. – David Stowell Feb 09 '17 at 18:50
  • Note, using either of `Response.Close();` or `Response.End();` will break downloading. Use `Response.Flush();` instead and then the `content-length` header is not required. See https://stackoverflow.com/a/736462/481207 – Matt Nov 12 '17 at 21:39