Reputation: 401
This is probably a really easy question, but I actually haven't seen many search results on this.
I have a very basic submit button within a form that takes some user input, and generates a downloadable file in the server's temp directory, then prompts the user to download this file, which is then disabled by this on submit:
<form action="Home" method="post" onsubmit="Submit.disabled = true; return true;">
...
<input type="submit" name="Submit" value="Submit" id="Submit" />
We need it to be disabled for a few seconds while the page creates the file then prompts the user to download it. Once the file has been created, it gives the following response back in our SelectionServlet.java so the browser can download this generated file such as:
if (Export.equals("PDF")){
response.setContentType(".pdf");
response.setHeader("Content-disposition", "attachment; filename="+Constants.FILE_NAME+".pdf");
File dlFile = new File(Constants.FILE_LOCATION+".pdf");
// This should send the file to browser
OutputStream outStream = response.getOutputStream();
FileInputStream in = new FileInputStream(dlFile);
byte[] buffer = new byte[4096];
int length;
while ((length = in.read(buffer)) > 0){
outStream.write(buffer, 0, length);
}
in.close();
outStream.flush();
Export="HTML";
}
After the file is ready to be downloaded, I'd like to re-enable that Submit button so the user can re-use the form data they put in (no page redirection was done, as the user basically just selects what criteria goes into the file they're building, and what file type it is, the Submit button just ultimately brings us to a java web connection that connects to a source and builds various file types into the temp dir of the server to be downloaded by the user).
I have played around in Chrome, and I can actually remove the disabled attribute on the submit button, then click the button again but with different criteria and get a different result back. What code can actually do this, I'm not sure.
Upvotes: 3
Views: 2834
Reputation: 1108782
Set a cookie on the response of the file download and let JavaScript check for the cookie on intervals. Once the file download is ready to be served and thus the Save As dialog thing is happening, then the cookie will be available to JavaScript. To ensure proper working across multiple browser windows/tabs in the same session, best is to generate an unique token in JavaScript, pass it as request parameter along to the download request and let the servlet set it as cookie value.
Basically, this should do:
<form action="Home" method="post" onsubmit="startDownload(this)">
...
<input type="hidden" name="token" />
<input type="submit" name="Submit" value="Submit" id="Submit" /> <!-- I'd rather rename and lowercase the ID/name. -->
</form>
With this JavaScript (when using jQuery, the jquery-cookie plugin may be helpful to reduce document.cookie
verbosity):
function startDownload(form) {
var token = new Date().getTime();
form.token.value = token;
form.Submit.disabled = true;
var pollDownload = setInterval(function() {
if (document.cookie.indexOf("download=" + token) > -1) {
document.cookie = "download=" + token + "; expires=" + new Date(0).toGMTString() + "; path=/";
form.Submit.disabled = false;
clearInterval(pollDownload);
}
}, 500);
}
And in the servlet:
// Prepare download here.
// ...
// Once finished preparing, set cookie.
Cookie cookie = new Cookie("download", request.getParameter("token"));
cookie.setPath("/");
response.addCookie(cookie);
// Now stream download to response.
// ...
Upvotes: 7