2

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.

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.

The code for setting cookie.

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

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.

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

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

Thanks in advance.

Jafar Ali
  • 1,084
  • 3
  • 16
  • 39

3 Answers3

1

you may make another request (with AJAX/jquery) on successCallback (if this is the plugin you're using), pointing to another servlet that set the cookie in the response.

Marco
  • 66
  • 3
1

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.

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.

Jafar Ali
  • 1,084
  • 3
  • 16
  • 39
0

Cookies are set using set-cookie header in HTTP protocol. This being a header it will not be possible to set the cookie at the end of a request when the download completes. You should find some other way to do this. May be track the download complete on the server and then detect that by polling the server. Other way is using long-polling/comet.

RaviH
  • 3,544
  • 2
  • 15
  • 14