【发布时间】:2015-08-27 06:00:19
【问题描述】:
我有下面的代码,它适用于小文件,但对于大文件,它会根据需要生成 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 { }
}
}
【问题讨论】:
标签: c# asp.net asynchronous large-files large-file-support