I am trying to generate .bin file from from REST API written in Swing from AngularJS.Following is the code.
var options = {
url: 'http://example.com/imageAPI',
method: 'POST',
headers: {
'Authentication': headerAndPostParams[0],
'Content-Type': 'application/json',
'Accept': 'application/octet-stream'
},
responseType: 'arraybuffer',
data: {
uniqueId: headerAndPostParams[1],
imageName: headerAndPostParams[2],
clientMacAddress: headerAndPostParams[3]
}
};
return $http(options).then(function(sucessResponse) {
if (sucessResponse.data != "" && sucessResponse.data.responseCode === undefined) {
download(sucessResponse.data, "image.bin", "application/octet-stream");
return true;
}
return false;
});
Found the solution. Actually $http service by default change the response to text. That doubles the size of image. To avoid it we need to add following parameter.
responseType: "blob"
$http({
url: 'http://example.com',
method: 'POST',
responseType: "blob",
headers: {
'Authentication': headerAndPostParams[0],
'Content-Type': 'application/json',
'Accept': 'application/octet-stream'
},
data: {
uniqueId: headerAndPostParams[1],
imageName: headerAndPostParams[2],
clientMacAddress: headerAndPostParams[3]
}
}).then(function(sucessResponse) {
if (sucessResponse.data != "" && sucessResponse.data.responseCode === undefined) {
var blob = new Blob([sucessResponse.data], {
type: "application/octet-stream"
});
download(blob, headerAndPostParams[2]);
return true;
}
return false;
}, function(response) {
return false;
});