2

I'm trying something similar to this: Detect when browser receives file download

I'm using Chromes Developer Tools to monitor my cookies and my problem is that the cookies I set in my server side code (java in my case) are only visible on the request. Not when I check the Resources tab in Chrome Dev Tools.

I think it might have something to do with the fact that my request is submitted by constructing a hidden form and submitting this (I'm using ExtJS 4.2.2).

All I can see in my Resources tab is the JSESSIONID cookie from Tomcat.

Can anyone help me set cookies from java that I can read in JavaScript after the request completes?

Screenshots:

enter image description here

enter image description here

Community
  • 1
  • 1
CJe
  • 1,928
  • 3
  • 24
  • 53
  • So are you basing this only off of the data a tool provides you, or did you in fact try to read the cookie using Javascript after the request completes and the cookie isn't there? Did you do a dump of the response that the server generates? What is in there? – Gimby Sep 29 '14 at 07:27
  • Yes I tried reading the cookie and document.cookie only returns the JSESSIONID cookie. – CJe Sep 29 '14 at 07:46
  • That leaves the "checking the response from the server" part of my questions. – Gimby Sep 29 '14 at 10:54
  • Not quite sure what u mean @Gimby. See screenshots in updated question. Why can't I see downloadCookie006 using document.cookie? – CJe Sep 29 '14 at 11:24

1 Answers1

1

There are lots of ways to do this but heres one that would allow you to drop the cookie approach. You really should be setting your cookies to HttpOnly for security reasons unless you have a use case that requires it.

  1. Expose your submit button in some sort of Iframe so users can submit the file while still remaining on the primary page.

  2. Detect when a click occurs in the iFrame and start polling through AJAX at some URL such as "/FileUploadStatus".

  3. On the server side once the file is completed upload set a session attribute such as

    HttpSession session = request.getSession(false); session.setAttribute("fileUploadStatus",true);

  4. When the AJAX request hits the servlet at /FileUploadStatus, check the session variable to see if the the file upload servlet has changed the value of fileUploadStatus to true. If so then return an indication to the client to stop polling and update the page and clear the session attribute on the server.

NOTE: Detecting a click in an iFrame is hairy stuff across various browsers. You might have to just start polling the second they access your "Upload Page" and simply wait.

Alternate Solution: You could also form a direct connection using the new WebSocket API. THis approach runs the fastest but not all browsers support websockets.

Usman Mutawakil
  • 4,993
  • 9
  • 43
  • 80
  • Thanks for your comment @Usman. Unfortunately I'm restricted in terms of how creative I can be due to other solutions using the same code. I will try to make a sample solution and see how far your suggestion takes me. Thanks! – CJe Sep 30 '14 at 07:06