0

I create .xlsx file on a server by using Apache POI library. I just can't understand whats steps I should to perform to download this file when user click on a button? This button invoke method in JAX-RS which create .xlsx, it didn't write it to server. Is it possible to download it directly to user (without writing to the server's disk), i.e. open popup where user being asked if he want to download this file?

I tried this solution but it doesn't work:

File file = new File("test.xlsx");

        ResponseBuilder response = Response.ok((Object) file);
        response.header("Content-Disposition",
            "attachment; filename=new-excel-file.xls");
        return response.build();

And ajax code:

$.ajax({
                  type: "POST",
                  url: 'api/export',
                  data: JSON.stringify(chartData),
                  headers : {
                        'Accept' : 'application/json',
                        'Content-Type' : 'application/json'
                  },
                  success: function(data){
                    alert(data);
                  }
            });

And I get error 406 error, I suspect that I should define that ajax requests accept returned data type, but don't know which data type is? Thank you.

Anatoly
  • 5,056
  • 9
  • 62
  • 136
  • I don't think you can do this as an AJAX call. You should open a new window with the download URL. Then the browser will prompt the user to save the file. – Thilo Feb 09 '15 at 14:03
  • @Thilo, actually I have done this using this SO question: http://stackoverflow.com/questions/16086162/handle-file-download-from-ajax-post, it's a little bit tricky because I actually call jax-rs service twice, first one I prepare the file and second one I download it. Unfortunately I didn't succeeded to implement second answer with **blob** (answer with bounty), file which I receive is corrupted. – Anatoly Feb 10 '15 at 08:50

0 Answers0