简体   繁体   中英

Java Servlet for file download: Works when started from form submit, not when started from jQuery download

I have a Java Servlet for File Download (taken from the examples on this page ( Implementing a simple file download servlet ) that works in general but not when started via query.

The setup works as follows: When clicking on a button (input type submit) a file is generated and stored on the server. The response is redirected to the file download servlet which then streams the content of the generated file to the response. This works perfectly fine, except when starting the download procedure via a jQuery post function call.

When looking at the requests the only difference I could find was the accept-header of the request. This was / for the jQuery post and 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp, / ;q=0.8' for the form submit request.

Could that be the decisive difference? And if so, how can I set what to accept?

This is the function call for the post in js:

$.post("path", {
            action : "action",
            ids: ids.toString()
        }, null, "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");

This is the part where I set the content-disposition:

resp.addHeader("content-disposition:",
                        "attachment; filename=name.csv");

Browsers don't handler XHR responses the same way that they handle "normal" HTTP requests. If your servlet is correctly setting the "Content-disposition" header, then you can just do an ordinary form post.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM