Failed - network error in get Zip file in chrome

1.3k Views Asked by At

I am creating a zip by Ionic.Zip.dll like this (ASP.NET,C#):

zip.AddEntry("Document.jpeg", File.ReadAllBytes("Path");

I want to download it like this:

Response.Clear();
Response.BufferOutput = false;
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "filename=SuppliersDocuments.zip";
zip.Save(Response.OutputStream);
Response.Close();

I tested this code in localhost by Firefox and Chrome and it worked properly. But when I test this code in host, I get this error:

Failed - network error

Is my code is wrong?

1

There are 1 best solutions below

0
On

I ran into a similar issue with relaying an SSRS report. Taking @Arvin's suggestion, I did the following:

private void CreateReport(string ReportFormat)
{
    ReportViewer rview = new ReportViewer();

    // (setup report viewer object)

    // Run report
    byte[] bytes = rview.ServerReport.Render(ReportFormat, deviceInfo, out mimeType, out encoding, out extension, out streamids, out warnings); 

    // Manually create a response
    Response.Clear();
    Response.ContentType = mimeType;
    Response.AddHeader("Content-disposition", string.Format("attachment; filename={0}.{1}", fileName, extension));

    // Ensure the content size is set correctly
    Response.AddHeader("Content-Length", bytes.Length.ToString()); // <- important

    // Write to the response body
    Response.OutputStream.Write(bytes, 0, bytes.Length);

    // (cleanup streams)
}

The fix was adding the Content-Length header and setting it to the size of the byte array from reporting services.