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

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
Add a ment  | 

1 Answer 1

Reset to default 7

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745674246a4639587.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信