问题
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:
- loading a view that
- has an
iframe
that - requests a file and
- 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:
- On the client generate some unique id.
- Send the request to the download action by passing this unique id.
- Use a
window.setInterval
to query for the presence of a cookie at regular intervals and check its value. - 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