java - Reenable a form submit button on response of a file download - Stack Overflow
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.
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.
Share Improve this question edited Oct 4, 2013 at 12:29 BalusC 1.1m376 gold badges3.7k silver badges3.6k bronze badges asked Oct 3, 2013 at 15:46 aohm1989aohm1989 4011 gold badge8 silver badges21 bronze badges 2- What's preventing it from going to the next page on submit? – developerwjk Commented Oct 3, 2013 at 21:01
- @developerwjk: OP is serving an attachment (a file download) on submit. – BalusC Commented Oct 4, 2013 at 12:17
1 Answer
Reset to default 7Set 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.
// ...
- 成本高、厂商疑 微软Surface面临十大风险
- 探秘2012台北国际电脑展(图)
- Intel毫不客气:ARM+Win8软硬件都不行
- 后PC时代是五足鼎立还是苹果独领风骚?
- python - Adding quotes to list objects to format as a dictionary pyspark - Stack Overflow
- react native - Handling loginlogout without user undefined errors - Stack Overflow
- Flutter - individual bloc states - Stack Overflow
- python - Can't run pytest with a `FileNotFoundError: [Errno 2] No such file or directory: 'tmppip-build-env-3fag
- rcpp - C++ AVX2 custom functions (e.g., "exp") not working on Windows (but work on Linux) - Stack Overflow
- html - Vertical Alignment of text inside a container, that is nested within another container - Stack Overflow
- smartcontracts - FailedCall() with OpenZeppelin meta transactions (ERC2771Forwarder and ERC2771Context) - Stack Overflow
- c# - How to add image as an input to a ChatMessage with Microsoft.Extensions.AI - Stack Overflow
- javascript - Is there any way of getting an error message from the browsers XslProcessor object when using xsl:message terminate
- Deploying Angular App on Azure SWA with SCSS Issues - Stack Overflow
- c# - Microsoft.Extensions.Logging.LoggerFactory how to prevent an extra date to be added to the log filename - Stack Overflow
- c++ - Member of struct constructed twice in custom constructor? - Stack Overflow
- python - Convert numpy float to string - Stack Overflow