1

我创建了一个下载按钮,它将压缩我文件夹中的文件,然后将压缩文件返回给用户。返回 zip 文件后,我无法刷新页面。

这是我的代码:

<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>

后面的代码

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

我怎样才能做一些事情,比如将 zip 返回给用户,我也刷新我的页面以显示我在数据库中更新的上传日志?

4

0 回答 0