仅在文件下载完成后设置cookie. [英] Set cookie only after file download complete.

查看:257
本文介绍了仅在文件下载完成后设置cookie.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,我想告诉用户下载已完成并提示关闭按钮.为此,我使用了一个jquery插件,该插件会持续监视cookie以了解此下载何时完成.

I have a scenario where I want to tell the user that download is completed and prompt a close button. For this I am using a jquery plugin which monitors a cookie continuously to know when this download completes.

我的问题是我想在下载完成后立即设置此cookie(fileDownload = true and path="/").为此,我尝试了各种位置来设置此Cookie.但是每次在下载之前就开始设置Cookie.

My problem is I want to set this cookie (fileDownload = true and path="/") as soon as the download gets completes. For this I tried various location for setting this cookie. But every time the cookie get set before the download even begin.

用于设置cookie的代码.

The code for setting cookie.

Cookie fileDwnld = new Cookie("fileDownload", "true");
fileDwnld.setPath("/");
response.addCookie(fileDwnld);

我在Servlet上进行设置,该Servlet也具有提取文件并将其通过输出流传输到浏览器的代码.

I am setting this on a Servlet which also has the code to fetch the file and stream it through output stream to the browser.

我唯一的问题是在下载文件之前先设置cookie.

My only problem is the cookie get set before the file get downloaded.

任何建议.我正在使用Tomcat7.Java1.6,Servlet.我正在使用的jquery插件是fileDownload.js(我相信这不是罪魁祸首).

Any suggestion. I am using Tomcat 7. Java 1.6, Servlet. The jquery plugin I am using is fileDownload.js (Which I believe is not a culprit).

谢谢.

推荐答案

我对RaviH和Marco提出的内容做了一些隐藏.我使用Jquery fileDownload插件监视下载开始的时间,然后相应地更改外观.然后在successCallback事件中,我启动一段JS代码,该代码不断轮询到服务器以获取下载状态.

I did some thing hibride of what RaviH and Marco proposed. I used the Jquery fileDownload Plugin to monitor when the download starts and then changing the visuals accordingly. Then on successCallback event I am start a piece of JS code which continuously polle to the server for the download status.

我通过在servlet中设置会话属性来实现此目的,该属性将文件推送通过流.会话属性名称包含随请求发送的随机字符串,对于我来说,我正在发送当前时间(以毫秒为单位),例如DOWNLOAD_STATUS_(Random Code).

I achived this by setting a session attribute in the servlet which pushes the file through stream. The session attributes name contains the random string sent with the request, In my case I am sending current Time in milliseconds e.g DOWNLOAD_STATUS_(Random Code).

Again when I am polling I am resending the same Random Code through ajax and get the status. The three status I am using are `STARTED`,`PROGRESS` and `COMPLETE`. 

在浏览器上,当我收到进度响应时,我会显示进度或加载动画,但是当我收到完整响应时,我进行了适当的更改以显示下载完成.

On browser I show the progress or loading animation while I get the Progress response but when i get the Complete Response I Made appopriate changes to show Download complete.

希望这对其他人有帮助.

Hope this help others.

这篇关于仅在文件下载完成后设置cookie.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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