1

I had create a download button, it will zip the files from my folder then it will return the zip file to user. I had no way to refresh my page after return the zip file.

Here was my code like this :

<div runat="server" id="divDownload">
    <table width="100%" class="border" cellpadding="2" cellspacing="1">
        <tr>
            <th colspan="5" align="left">Download</th>
        </tr>          
        <tr>
            <td></td>
            <td>
                <asp:Button runat="server" ID="btnDownloadAll" Text="Download All Converted" OnClick="btnDownloadAll_Click" /></td>
        </tr>
    </table>
</div>

code behind

protected void btnDownloadAll_Click(object sender, EventArgs e)
{
    // Here we will create zip file & download
    Response.ContentType = "application/zip";
    Response.AddHeader("content-disposition", "fileName=test.zip");
    byte[] buffer = new byte[4096];
    ZipOutputStream zipOutputStream = new ZipOutputStream(Response.OutputStream);
    zipOutputStream.SetLevel(3);
    try
    {
        DirectoryInfo DI = new DirectoryInfo(Server.MapPath("~/myFolder/"));
        foreach (var i in DI.GetFiles())
        {
            Stream fs = File.OpenRead(i.FullName);
            ZipEntry zipEntry = new ZipEntry(ZipEntry.CleanName(i.Name));
            zipEntry.Size = fs.Length;
            zipOutputStream.PutNextEntry(zipEntry);
            int count = fs.Read(buffer, 0, buffer.Length);
            while (count > 0)
            {
                zipOutputStream.Write(buffer, 0, count);
                count = fs.Read(buffer, 0, buffer.Length);
                if (!Response.IsClientConnected)
                {
                    break;
                }
                Response.Flush();
            }
            fs.Close();
        }
        zipOutputStream.Close();
        Response.Flush();
        Response.End();
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

How can I do things like return the zip to user, also i refresh my page for showing the uploadlog that I updated in database?

  • You cannot. You can only send one response to the client. So either a File or a webpage with an updated UI. If you want both you need to use something like Ajax. – VDWWD Aug 03 '19 at 08:34
  • Can you explain it further on how to use Ajax for return zip and refresh? – Jessie Loke Aug 03 '19 at 09:14

0 Answers0