我有下面的代码,它适用于小文件,但对于大文件,它会根据需要生成 zip,但不会下载它。我得到了各种各样的错误,包括超时(我已经设法解决了)。另一个问题是它在同步中运行。我自己生成的最大文件是一个 330MB 的 zip 文件,其中附有大约 30 张高清图像。但这甚至可以达到 GB,因为用户可以选择一次下载大约 100 个甚至更多的高清图像。
为了解决这两个问题,我认为下载async
可能对这两种情况都有帮助。我想提醒用户他们的下载已经开始,并且他们会在下载准备好时收到通知。
我正在考虑在客户端发送流IsConnected
(然后删除文件)或发送电子邮件以要求他们在决定注销时下载文件(然后使用离线下载链接删除文件)。我只是不知道在哪里或如何编写async
代码,或者如果用户决定注销,我想要做什么实际上可以完成。
这是我当前的代码:
private void DownloadFile(string filePath)
{
FileInfo myfile = new FileInfo(filePath);
// Checking if file exists
if (myfile.Exists)
{
// Clear the content of the response
Response.ClearContent();
// Add the file name and attachment, which will force the open/cancel/save dialog box to show, to the header
Response.AddHeader("Content-Disposition", "attachment; filename=" + myfile.Name);
// Add the file size into the response header
Response.AddHeader("Content-Length", myfile.Length.ToString());
// Set the ContentType
Response.ContentType = "application/octet-stream";
Response.TransmitFile(filePath);
Response.Flush();
try
{
myfile.Delete();
}
catch { }
}
}