如何在ASP.NET中检测文件下载何时完成? [英] How can I detect when a file download has completed in ASP.NET?

查看:140
本文介绍了如何在ASP.NET中检测文件下载何时完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个弹出窗口,显示请等待文件下载中".该弹出窗口还将执行以下代码以开始文件下载.文件下载完成后,如何关闭弹出窗口?我需要某种方法来检测文件下载已完成,因此我可以调用self.close()关闭此弹出窗口.

I have a popup window that displays "Please wait while your file is being downloaded". This popup also executes the code below to start the file download. How can I close the popup window once the file download has completed? I need some way to detect that the file download has completed so I can call self.close() to close this popup.

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();

推荐答案

有一种解决方案,您可以通过将文件作为较小的数据包传输来跟踪下载状态,并检查是否已传输所有数据包.解决方案不是我的,但是您可以在这里找到它:在ASP中下载文件.NET和跟踪下载成功/失败状态

There is a solution where you can track the download status by transferring the file as smaller packets and check whether all the packets have been transferred. The solution is not mine but you can find it here: 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;
}

这篇关于如何在ASP.NET中检测文件下载何时完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆