How do you move a zip file using the image button in asp.net?

My problem: when the user clicks the image button on the aspx page, codebehind creates a zip file, and then I try to transfer this zip file to the user.

To transfer the file, I use the following code:

FileInfo toDownload = new FileInfo(fullFileName);
if (toDownload.Exists)
{
   Response.Clear();
   Response.ContentType = "application/zip";
   Response.AppendHeader("Content-Disposition", "attachment;filename=" +
             toDownload.Name);
   Response.AppendHeader("Content-Length", toDownload.Length.ToString());
   Response.TransmitFile(fullFileName);
   HttpContext.Current.ApplicationInstance.CompleteRequest();
}

When I try to accomplish this, I get the following error on the page:

Sys.WebForms.PageRequestManagerParserErrorException: A message received from the server cannot be parsed. Common causes of this error are changes in response to Response.Write () calls, response filters, HttpModules, or server tracing. Details: parsing error next to "PK ...".

PK - zip , zip , , zip . , , / zip , , .

?

EDIT: , .

+3
3

, , PDF ( / ):

        byte[] rendered = uxReportViewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);

        Response.Buffer = true;
        Response.Clear();
        Response.ClearHeaders();
        Response.ContentType = mimeType;
        Response.CacheControl = "public";
        Response.AddHeader("Pragma", "public");
        Response.AddHeader("Expires", "0");
        Response.AddHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
        Response.AddHeader("Content-Description", "Report Export");
        Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename + "." + extension + "\"");

        Response.BinaryWrite(rendered);
        Response.Flush();
        Response.End();

zip , , , .

+2

, , , , , , : UpdatePanel.

PostBackTrigger :

<Triggers>
    <asp:PostBackTrigger ControlID="ibDownload" />
</Triggers>
+1

Man, don't you use DotNetZip to generate zip files? What if you did not create a zip file on disk, but only in memory? This example uses dotnetzip to do this.

    Response.Clear();
    Response.BufferOutput = false;
    String ReadmeText= "This is content that will appear in a file " + 
                       "called Readme.txt.\n" + 
                       System.DateTime.Now.ToString("G") ; 
    string archiveName= String.Format("archive-{0}.zip", 
                                      DateTime.Now.ToString("yyyy-MMM-dd-HHmmss")); 
    Response.ContentType = "application/zip";
    Response.AddHeader("content-disposition", "attachment; filename=" + archiveName);

    using (ZipFile zip = new ZipFile())
    {
        // add an entry from a string: 
        zip.AddEntry("Readme.txt", "", ReadmeText);
        zip.AddFiles(filesToInclude, "files");
        zip.Save(Response.OutputStream);
    }
    // Response.End();  // no - see http://stackoverflow.com/questions/1087777
    HttpContext.Current.ApplicationInstance.CompleteRequest();
0
source

Source: https://habr.com/ru/post/1704347/


All Articles