如何使用 DownloadFileCompletein c#
本文关键字:DownloadFileCompletein 何使用 | 更新日期: 2023-09-27 18:33:47
下面的代码工作正常。创建一个 zip 文件,弹出一个对话框并要求您保存 zip。
我想在关闭对话框下载后做一些事情而不保存zip。
有一个DownloadFileCompleted
语句,但是您如何在我的方法中使用它?
req.DownloadFileCompleted(...);
>>>我把它放在哪里。
protected void CreateAndSaveZipFile(string path)
{
string startPath = path;
string zipPath = Server.MapPath("Export");
DateTime now = DateTime.Now;
zipPath += now.ToString(@"'ddMMyyyyhhmmtt", new CultureInfo("en-GB"));
zipPath += @"_Export.zip";
ZipFile.CreateFromDirectory(startPath, zipPath);
string zipName = @"Export_" + now.ToString(@"'ddMMyyyyhhmmtt", new CultureInfo("en-GB")) +".zip";
WebClient req = new WebClient();
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.ClearContent();
response.ClearHeaders();
response.Buffer = true;
response.AddHeader("Content-Disposition", "attachment;filename='"" + zipName);
byte[] data = req.DownloadData(zipPath);
response.BinaryWrite(data);
response.End();
}
有人可以告诉我如何检查对话框是否已关闭或文件是否已成功下载。
来自 MSDN:DownloadFileCompleted
的描述:
每次异步文件下载操作时都会引发此事件 完成。异步文件下载是通过调用 下载文件异步方法。
所以我在你的代码中看到至少两个问题:
首先:您使用的是同步方法DownloadData
而不是异步 DownloadDataAsync
第二:您正在尝试处理不正确的事件。它应该是"下载数据已完成"事件。
摘要:使用DownloadDataAsync
方法并处理DownloadDataCompleted
事件。处理DownloadDataCompleted
的示例可以在这里看到