如何检测在ASP.NET中文件下载何时完成?
我有一个弹出窗口,显示“正在下载文件时请稍候”。该弹出窗口也执行下面的代码来开始文件下载。文件下载完成后,如何关闭弹出窗口?我需要一些方法来检测文件下载已完成,所以我可以调用self.close()来关闭此弹出窗口。如何检测在ASP.NET中文件下载何时完成?
System.Web.HttpContext.Current.Response.ClearContent();
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.ClearHeaders();
System.Web.HttpContext.Current.Response.ContentType = fileObject.ContentType;
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", string.Concat("attachment; filename=", fileObject.FileName));
System.Web.HttpContext.Current.Response.WriteFile(fileObject.FilePath);
Response.Flush();
Response.End();
一些黑客周围,涉及当缓冲区的最后一块已发送知道或检查HttpResponse.IsClientConnected
财产。
我在Javascript中以不同的方式处理问题,这可能会或可能不适合您。
- 创建一个隐藏的DIV元素,与 消息“文件正在下载...” ,而不是一个弹出框。
- 显示div时下载 开始
- 一旦在 构成任何其他元素被点击时,隐藏在div 再次..
- 你也可以设置一个计时器,经过这么隐藏 下载消息的div 多的时间...
我的身影曾经另一元件上,用户点击,她要么已经知道下载完成,或者她正准备做别的事情,所以该消息变得无关紧要,可以走....
一个想法:
如果处理文件由一块块写入到响应流下载自己在服务器端的代码,那么你会知道什么时候该文件已经下载完毕。您只需将FileStream连接到响应流,按块发送数据块,并在完成后重定向。这可以在你的弹出窗口中。
Response.ContentType = "application/octet-stream";
Response.AppendHeader("content-disposition", "attachment; filename=bob.mp3");
Response.AppendHeader("content-length", "123456789");
确保在写出响应流时检查Response.IsClientConnected。
要做到这一点的方法是在弹出窗口中通过AJAX轮询调用服务器,以获得某些指示文件被刷新的响应。
例如:在发送文件之前,将会话ID +文件名存储在数据库或会话中或您有什么。
在客户端,在弹出窗口中,通过轮询AJAX web服务 - 这甚至也能像Bool IsContentFlushed(string sessionID, string fileName);
一个WebMethod你Response.Flush();
从存储中删除此会话ID +文件名后。
拨打电话Response.Close()
而不是Response.End()
- 后者非常残忍,通常是过度杀人。
有一个解决方案,您可以通过传输文件作为较小的数据包来跟踪下载状态,并检查是否所有数据包都已传输。 该解决方案是不是我的,但你可以在这里找到: File Download in ASP.NET and Tracking the Status of Success/Failure of Download
//Function for File Download in ASP.Net in C# and
//Tracking the status of success/failure of Download.
private bool DownloadableProduct_Tracking()
{
//File Path and File Name
string filePath = Server.MapPath("~/ApplicationData/DownloadableProducts");
string _DownloadableProductFileName = "DownloadableProduct_FileName.pdf";
System.IO.FileInfo FileName = new System.IO.FileInfo(filePath + "\\" + _DownloadableProductFileName);
FileStream myFile = new FileStream(filePath + "\\" + _DownloadableProductFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
//Reads file as binary values
BinaryReader _BinaryReader = new BinaryReader(myFile);
//Ckeck whether user is eligible to download the file
if (IsEligibleUser())
{
//Check whether file exists in specified location
if (FileName.Exists)
{
try
{
long startBytes = 0;
string lastUpdateTiemStamp = File.GetLastWriteTimeUtc(filePath).ToString("r");
string _EncodedData = HttpUtility.UrlEncode(_DownloadableProductFileName, Encoding.UTF8) + lastUpdateTiemStamp;
Response.Clear();
Response.Buffer = false;
Response.AddHeader("Accept-Ranges", "bytes");
Response.AppendHeader("ETag", "\"" + _EncodedData + "\"");
Response.AppendHeader("Last-Modified", lastUpdateTiemStamp);
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName.Name);
Response.AddHeader("Content-Length", (FileName.Length - startBytes).ToString());
Response.AddHeader("Connection", "Keep-Alive");
Response.ContentEncoding = Encoding.UTF8;
//Send data
_BinaryReader.BaseStream.Seek(startBytes, SeekOrigin.Begin);
//Dividing the data in 1024 bytes package
int maxCount = (int)Math.Ceiling((FileName.Length - startBytes + 0.0)/1024);
//Download in block of 1024 bytes
int i;
for (i = 0; i < maxCount && Response.IsClientConnected; i++)
{
Response.BinaryWrite(_BinaryReader.ReadBytes(1024));
Response.Flush();
}
//if blocks transfered not equals total number of blocks
if (i < maxCount)
return false;
return true;
}
catch
{
return false;
}
finally
{
Response.End();
_BinaryReader.Close();
myFile.Close();
}
}
else System.Web.UI.ScriptManager.RegisterStartupScript(this, GetType(),
"FileNotFoundWarning","alert('File is not available now!')", true);
}
else
{
System.Web.UI.ScriptManager.RegisterStartupScript(this, GetType(),
"NotEligibleWarning", "alert('Sorry! File is not available for you')", true);
}
return false;
}
简短的回答......你不能! – 2011-01-13 02:15:50