Is it possible to detect whether file actually downloaded

旧街凉风 提交于 2019-12-13 15:24:16

问题


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...

My thinking

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. 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.

The main question being

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


回答1:


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

  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.

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




回答2:


Please visit this url

http://odetocode.com/blogs/scott/archive/2010/06/23/checking-client-download-success-with-asp-net-mvc.aspx

hope this helps to you



来源:https://stackoverflow.com/questions/10053326/is-it-possible-to-detect-whether-file-actually-downloaded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!