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]
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.
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.
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).
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.
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' });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With