如何判断文件是否已下载?

发布于 2024-08-07 08:06:53 字数 505 浏览 5 评论 0原文

我有以下代码,允许用户下载文件。我需要知道(如果可能的话)他们是否成功下载了文件。是否有任何类型的回调可以让我知道他们是否成功下载了它?

string filename = Path.GetFileName(url);
context.Response.Buffer = true;
context.Response.Charset = "";
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.ContentType = "application/x-rar-compressed";
context.Response.AddHeader("content-disposition", "attachment;filename=" + filename);
context.Response.TransmitFile(context.Server.MapPath(url));
context.Response.Flush();

I have the following code that allows a user to download a file. I need to know (if possible) if they downloaded the file successfully. Is there any kind of callback that I can tie into to know whether or not they were successful in downloading it?

string filename = Path.GetFileName(url);
context.Response.Buffer = true;
context.Response.Charset = "";
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.ContentType = "application/x-rar-compressed";
context.Response.AddHeader("content-disposition", "attachment;filename=" + filename);
context.Response.TransmitFile(context.Server.MapPath(url));
context.Response.Flush();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

屋檐 2024-08-14 08:06:53

为什么不再添加一行让您知道它已经完成呢?在 context.Response.Flush() 之后,应该完成。

Why not add one more line that lets you know it's finished? After the context.Response.Flush(), it should be done.

硪扪都還晓 2024-08-14 08:06:53

你可以这样做:


try
{
    Response.Buffer = false;
    Response.AppendHeader("Content-Disposition", "attachment;filename=" + file.Name);
    Response.AppendHeader("Content-Type", "application/octet-stream");
    Response.AppendHeader("Content-Length", file.Length.ToString());

    int offset = 0;
    byte[] buffer = new byte[64 * 1024]; // 64k chunks
    while (Response.IsClientConnected && offset < file.Length)
    {
        int readCount = file.GetBytes(buffer, offset,
            (int)Math.Min(file.Length - offset, buffer.Length));
        Response.OutputStream.Write(buffer, 0, readCount);
        offset += readCount;
    }

    if(!Response.IsClientConnected)
    {
        // Cancelled by user; do something
    }
}
catch (Exception e)
{
    throw new HttpException(500, e.Message, e);
}
finally
{
    file.Close();
}

You can do something like this:


try
{
    Response.Buffer = false;
    Response.AppendHeader("Content-Disposition", "attachment;filename=" + file.Name);
    Response.AppendHeader("Content-Type", "application/octet-stream");
    Response.AppendHeader("Content-Length", file.Length.ToString());

    int offset = 0;
    byte[] buffer = new byte[64 * 1024]; // 64k chunks
    while (Response.IsClientConnected && offset < file.Length)
    {
        int readCount = file.GetBytes(buffer, offset,
            (int)Math.Min(file.Length - offset, buffer.Length));
        Response.OutputStream.Write(buffer, 0, readCount);
        offset += readCount;
    }

    if(!Response.IsClientConnected)
    {
        // Cancelled by user; do something
    }
}
catch (Exception e)
{
    throw new HttpException(500, e.Message, e);
}
finally
{
    file.Close();
}
心是晴朗的。 2024-08-14 08:06:53

我想这是不可能的。

响应只是与 IIS 交互的内存对象。您无法知道浏览器是否完全下载了文件,因为用户可能会在最后一个字节到达之前、IIS 完成发送整个流之后取消。

您可能会尝试实现 IHttpHandler,在 Process() 方法中连续将文件块写入 context.Response ,然后 Flush() 并像这样检查

context.Response.Flush();
if (!context.Response.IsClientConnected)
// handle disconnect

这是我能想到的最接近解决您的问题的方法。

I guess this is not possible.

Response is just a memory object which interacts with IIS. You cannot know whether the browser completely downloaded a file, since the user might cancel just before the last byte arrived, but after IIS finished sending the whole stream.

You might try to implement an IHttpHandler, continuously write chunks of the file to context.Response in the Process() method, and Flush() and check like this

context.Response.Flush();
if (!context.Response.IsClientConnected)
// handle disconnect

That's the closest thing I can think of solving your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文