Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download Excel file using jquery ajax

Hi i want to download XLX file using spring mvc ajax call.Below is my ajax call to server.

 $.ajax({
    type : 'GET',
    url : 'downloadExcel',
    beforeSend : function() {
        startPreloader();
    },
     complete: function(){
         stopPreloader();
     },
    success : function(response){
        console.log(response);
            var blob = new Blob([response], { type: 'application/vnd.ms-excel' });
            var downloadUrl = URL.createObjectURL(blob);
            var a = document.createElement("a");
            a.href = downloadUrl;
            a.download = "downloadFile.xlsx";
            document.body.appendChild(a);
            a.click();
    }
});

Here is my server code

@RequestMapping(value = "/downloadExcel", method = RequestMethod.GET)
    @ResponseBody
    public List<LicenceType> downloadExcel() {
        return licenceTypeService.findAllLicenceType();
    }

My code actually download the excel file, but on the excel sheet it showing like [Object][Object]

like image 513
boycod3 Avatar asked Aug 23 '16 07:08

boycod3


People also ask

How do I download an Excel file using Ajax?

Downloading Excel File using AJAX in JavaScript When the Download Button is clicked, the DownloadFile JavaScript function is called. Inside the DownloadFile JavaScript function, the URL of the File is passed as parameter to the GET call of the JavaScript XmlHttpRequest call.

How to download Excel file on button click using JavaScript?

Here Mudassar Ahmed Khan has explained with an example, how to download Excel File on Button click using JavaScript. The Excel file will be downloaded as BLOB using XmlHttpRequest AJAX call and then will be sent for download in the Browser using JavaScript.

How to download a PDF file using jQuery Ajax?

When the Download Button is clicked, the DownloadFile JavaScript function is called. Inside the DownloadFile JavaScript function, the URL of the File is passed as parameter to the jQuery AJAX function. Inside the jQuery AJAX function, using the XmlHttpRequest (XHR) call, the PDF file is downloaded as Byte Array (Binary Data).

How to download a file using JavaScript XMLHttpRequest?

When the Download Button is clicked, the DownloadFile JavaScript function is called. Inside the DownloadFile JavaScript function, the URL of the File is passed as parameter to the GET call of the JavaScript XmlHttpRequest call.


1 Answers

I know it's been almost a year but it worked for me with this:

var blob = new Blob([response], { type: 'data:application/vnd.ms-excel' }); 
like image 57
Julien Avatar answered Oct 20 '22 11:10

Julien