如何判断文件是否已下载?
我有以下代码,允许用户下载文件。我需要知道(如果可能的话)他们是否成功下载了文件。是否有任何类型的回调可以让我知道他们是否成功下载了它?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不再添加一行让您知道它已经完成呢?在 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.
你可以这样做:
You can do something like this:
我想这是不可能的。
响应只是与 IIS 交互的内存对象。您无法知道浏览器是否完全下载了文件,因为用户可能会在最后一个字节到达之前、IIS 完成发送整个流之后取消。
您可能会尝试实现 IHttpHandler,在 Process() 方法中连续将文件块写入 context.Response ,然后 Flush() 并像这样检查
这是我能想到的最接近解决您的问题的方法。
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
That's the closest thing I can think of solving your problem.