I have a http service which returns a HTTP response of type
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
and if I call this HTTP service from curl and redirect I get the .xlsx file recreated correctly. However trying to save the file from javascript fails. The code that I am using:
cURL:
$ curl -v -X POST <API_ENDPOINT> > a.xlsx ;# a.xlsx works fine
Javascript:
$http.post(<API_ENDPOINT>).then(function(response) {
console.log(response);
downloadFile(response.data, "b.xlsx")
});
var downloadFile = function(responseData, fileName) {
var blob = new Blob([responseData], {
type: 'application/vnd.ms-excel'
});
if (window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveBlob(blob, fileName);
} else {
var a = document.createElement('a');
var url = window.URL.createObjectURL(blob);
document.body.appendChild(a);
a.style = 'display: none';
a.href = url;
a.download = fileName;
a.click();
window.URL.revokeObjectURL(url);
document.body.removeChild(a);
}
};
The file is saved, however the contents seem to be different from the curl one, even though the server is sending the same file.
The content-length header value is the same for both the responses (curl and javascript (captured via chrome devtools)) but the filesize when redirected via curl is 5.1k (almost same as the content-length value) but the the filesize of file created via js, is 8.5k and is wrong.
I've tried setting application/octet-stream, octet/stream, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet while creating the blob and none of that helps.
I think I am messing up something in content-type and the blob creation but not able to figure out what is wrong. Any help ?