birdy
birdy

Reputation: 9636

How to download excel from jQuery get ajax request

I have a spring MVC view which provides an excel file, however, I've now modified the process so that the user gets a modal box in which they can select some options before downloading the excel. These selected options are sent to the view.

My request looks like this

$.get("downloadExcel", {  
    'foo': 'bar'
});

When that request is made I expect the file to be downloaded, however, that is not happening. Even though the response headers from the above request are:

Cache-Control:private, must-revalidate
Content-disposition:attachment; filename=filename.xls
Content-Language:en-US
Content-Type:application/vnd.ms-excel; charset=ISO-8859-1
Pragma:private
Server:Jetty(6.1.14)
Transfer-Encoding:chunked 

Note: All works well when I remove the dialog box so the request is no longer an ajax request.

Question

Is there a way to fetch excel file via an ajax request?

Upvotes: 2

Views: 2755

Answers (1)

rbaker86
rbaker86

Reputation: 1822

You can't do this using an Ajax response. You either need to redirect the browser or render a hidden iFrame in the page to initiate the download.

Upvotes: 2

Related Questions