In my application in Angular JS for downloading an Excel workbook through $http post.
I am using the below code and passing the information in the form of JSON as payload, and send it to the server REST web service (using Java Spring REST) through an angular $http post web service uses the information from the JSON and produces an Excel workbook with the format of application/vnd.ms-excel. If I try to save the response from POSTMAN for backend route test using "Send and Save" option the excel file is saving with the proper format(if I provide the response.xls). But using Angular JS the response within the success body of $http post, I'm getting binary data within that response variable and using FileSaver it is saving as binary data.
myservice.getExcel = function (searchParam) {
var data= $http.post('http:localhost:8080/export/xls',
{
searchParam: searchParam,
}
,
{headers: {
'Content-type': 'application/json'
}},{ responseType: 'arraybuffer' }).success(function (response, status, headers, config) {
console.log(response);
var blob = new Blob([response], { type: 'application/vnd.ms-excel' }),
fileName = 'MyReport.xls';
if (window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(blob, fileName);
} else {
var objectUrl = URL.createObjectURL(blob);
window.open(objectUrl);
var anchor = angular.element('<a/>');
anchor.css({ display: 'none' });
angular.element(document.body).append(anchor);
anchor.attr({
href: (window.URL || window.webkitURL).createObjectURL(blob),
target: '_blank',
download: fileName
})[0].click();
anchor.remove();
}
}).error(function (data, status, header, config) {
console.log("Error"+data);
});
I have FileSaver also but getting same result. Please suggest how to resolve this issue.
Before opening the excel I am getting as "The file you are trying to open "xls", is in a different format than specified by the file extension.Verify that file is not corrupted"
The response is coming as bytes.
I am getting the below data in excel file like this,
��ࡱ�;��
�������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� ��A����\p B�a=���=h:�#8X@�"��1���Arial1���Arial1���Arial1���Arial1���Arial"$"#,##0_);("$"#,##0)"$"#,##0_);Red "$"#,##0.00_);("$"#,##0.00)% "$"#,##0.00_);Red,'_( #,##0_);(* (#,##0);(* "-");(@)5)0("$"* #,##0_);("$"* (#,##0);("$"* "-");(@)=,8("$"* #,##0.00_);("$"* (#,##0.00);("$"* "-"??);(@)4+/(* #,##0.00_);(* (#,##0.00);(* "-"??);(@_)��� � ��� �� ��� �� ��� �� ��� �� ��� �� ��� �� ��� �� ��� �� ��� �� ��� �� ��� �� ��� �� ��� �� ��� �� � � �+�� �� �)�� �� �,�� �� �*�� �� � �� �� �