0

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");
Community
  • 1
  • 1
Display name
  • 2,697
  • 2
  • 31
  • 49
  • 1
    (taken from the examples on this page) link is missing – codefreaK May 02 '14 at 13:54
  • 1
    [I don't think you can do that; the browser is simply not going to pay attention to an attachment.](http://stackoverflow.com/questions/4552734/return-attachment-with-ajax-call) Just use an ordinary form post. – Pointy May 02 '14 at 13:56
  • response.setHeader( "Content-Disposition", "attachment; filename=\"" + fileName() + "\"" ); What i meant to ask was whether you have set the above line in servlet then irrespective of the way its called box will popup and download will occur – codefreaK May 02 '14 at 14:05
  • @user3127499 I adapted the question, I do set the header, however this seems to be ignored for some reason – Display name May 02 '14 at 14:27
  • @Pointy Thanks, with a normal form request (also via JS (http://stackoverflow.com/questions/133925/javascript-post-request-like-a-form-submit) it worked) If you post your comment as an answer I can accept it – Display name May 02 '14 at 15:00

1 Answers1

0

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.

Pointy
  • 405,095
  • 59
  • 585
  • 614