C# 如何创建下载文件并刷新页面
我已经创建了一个下载按钮,它将从我的文件夹压缩文件,然后它将返回压缩文件给用户。返回zip文件后,我无法刷新页面 我的代码如下:C# 如何创建下载文件并刷新页面,c#,asp.net,download,code-behind,zipstream,C#,Asp.net,Download,Code Behind,Zipstream,我已经创建了一个下载按钮,它将从我的文件夹压缩文件,然后它将返回压缩文件给用户。返回zip文件后,我无法刷新页面 我的代码如下: <div runat="server" id="divDownload"> <table width="100%" class="border" cellpadding="2" cellspacing="1"> <tr> <th colspan="5" align="left"&
<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>
如何将zip文件返回给用户,以及刷新页面以显示在数据库中更新的上载日志等操作?您不能。您只能向客户端发送一个响应。因此,要么是一个文件,要么是一个具有更新UI的网页。如果你想两者兼而有之,你需要使用类似Ajax的东西。你能进一步解释一下如何使用Ajax进行返回压缩和刷新吗?你不能。您只能向客户端发送一个响应。因此,要么是一个文件,要么是一个具有更新UI的网页。如果你想两者兼而有之,你需要使用类似Ajax的东西。你能进一步解释一下如何使用Ajax进行返回压缩和刷新吗?
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;
}
}