我正在尝试从AngularJS中用Swing编写的REST API生成.bin文件。以下是代码。
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;
});
答案 0 :(得分:0)
找到解决方案。实际上$ http服务默认情况下会将响应更改为文本。这使图像的大小翻倍。为了避免它,我们需要添加以下参数。
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;
});