是否可以检测文件是否实际下载 [英] Is it possible to detect whether file actually downloaded

查看:74
本文介绍了是否可以检测文件是否实际下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个返回FileResult的控制器动作方法.是否可以检测文件是否实际下载完全下载到客户端?

Suppose I have a controller action method that returns FileResult. Is it possible to detect whether file was actually downloaded completely to the client?

public ActionResult GetFile(int id)
{
    DownloadInfo data = provider.GetInfo(id);
    this.provider.MarkDownloadStart(id);

    return File(Server.MapPath(data.Filename), "application/pdf");
}

我确实在调用操作时存储信息,但这仅意味着有人启动了下载.下载是否完成,服务器上没有任何信息.

I do store information when action is being invoked, but that only means that someone initiated download. It doesn't give me any information on the server whether download completed...

我还设置了内容处理标头,因此据说文件永远不会在浏览器中直接打开,而是浏览器要求打开/保存它.这样可以使客户端更轻松地保存文件...

I also set content disposition header so file is supposedly never opened directly in browser but rather browser asks to open/save it. This makes it easier for the client to save the file...

我想不可能直接检测到实际上已经进行了下载.但是否可以通过某种方式进行其他操作:

I suppose it's not possible to directly detect that download has actually taken place. but is it possible to do it some other way as by somehow:

  1. 加载一个视图
  2. 具有一个iframe
  3. 请求文件和
  4. 主视图监督在客户端上的下载,因为它可以访问iframe,因此可以查看文件是否已完成下载.
  1. loading a view that
  2. has an iframe that
  3. requests a file and
  4. the main view supervises download on the client because it has access to iframe so it sees whether file has finished downloading or not.

尽管上层方案看似可行,但我不知道该如何实际执行监督.也许还有其他更好的方法可以完成此任务.

Even though upper scenario may seem feasible, I don't know how to actually do this supervision. Maybe there's some other - better - way of doing this task.

是否可以在服务器上检测到客户端完全完成了文件下载并且在下载之间没有取消文件下载?

Is it possible to detect on the server that the client finished file downloading completely and not cancelling it in between download?

推荐答案

您可以使用cookie.这是

You could use cookies. Here's an example. Here are some of the steps involved in the process:

  1. 在客户端上生成一些唯一的ID.
  2. 通过传递此唯一ID将请求发送到下载操作.
  3. 使用window.setInterval定期查询cookie的存在并检查其值.
  4. 如果创建了cookie,并且其值与唯一ID之一匹配,则下载已完成.
  1. On the client generate some unique id.
  2. Send the request to the download action by passing this unique id.
  3. Use a window.setInterval to query for the presence of a cookie at regular intervals and check its value.
  4. If the cookie is created and its value matches the one of the unique id then download has finished.

在将文件流式传输到响应后,在服务器上将cookie值设置为唯一ID.

And on the server set the cookie value to the unique id once you have finished streaming the file to the response.

这篇关于是否可以检测文件是否实际下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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