[英]How can I detect when a file download has completed in ASP.NET?
我有一個彈出窗口 window 顯示“正在下載您的文件,請稍候”。 此彈出窗口還執行以下代碼以開始文件下載。 文件下載完成后,如何關閉彈出窗口 window? 我需要一些方法來檢測文件下載已經完成,所以我可以調用 self.close() 來關閉這個彈出窗口。
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();
一個主意:
如果通過將文件逐塊寫入響應流來使用服務器端代碼自己處理文件下載,那么您將知道文件何時完成下載。 您只需要將FileStream連接到響應流,逐塊發送數據,並在完成后重定向。 這可以在您的彈出窗口中。
Response.ContentType = "application/octet-stream";
Response.AppendHeader("content-disposition", "attachment; filename=bob.mp3");
Response.AppendHeader("content-length", "123456789");
寫入響應流時,請確保選中Response.IsClientConnected。
有一個解決方案,您可以通過將文件作為較小的數據包傳輸來跟蹤下載狀態,並檢查是否已傳輸所有數據包。 解決方案不是我的,但您可以在這里找到它: 在ASP.NET中下載文件並跟蹤下載成功/失敗的狀態
//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;
}
周圍存在一些黑客攻擊,其中包括知道何時發送了緩沖區的最后一塊,或者檢查HttpResponse.IsClientConnected
屬性。
我用Javascript處理問題的方式有所不同,這可能對您不起作用。
我認為一旦用戶單擊另一個元素,她要么已經知道下載已完成,要么准備進行其他操作,因此該消息變得無關緊要並可以消失。
這樣做的方法是在彈出窗口中通過AJAX輪詢調用服務器以獲取某些響應,該響應表明文件已刷新。
例如:在發送文件之前,將sessionID + FileName存儲在數據庫或會話中,或者您擁有什么。
在客戶端的彈出窗口中,通過AJAX輪詢網絡服務-甚至可以是類似於Bool IsContentFlushed(string sessionID, string fileName);
的WebMethod Bool IsContentFlushed(string sessionID, string fileName);
完成Response.Flush();
從您的商店中刪除此sessionID + FileName。
調用Response.Close()
而不是Response.End()
-后者非常殘酷,並且通常會被殺死。
即使這是一個古老的問題,也一直沒有得到回答,我相信它應該(更好)回答:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.