2017-04-18 24 views
1

我想从AngularJS的Swing中编写的REST API生成.bin文件。以下是代码。图像大小(.bin)通过AJAX调用生成时翻倍

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; 
 
});

下面是响应头
访问控制允许来源:http://localhost:8080
内容处置:附件;文件名= cns3xxx_md_2.6.9_ac_aa_33_dd_aa_35.bin
内容长度:7864320
Content-Type:application/octet-stream
日期:2017年4月18日(星期二)06:38:35 GMT
各不相同:产地
访问控制允许的凭据:真
上面的代码工作fine.But问题是API发送图片的大小为7.5 MB,并从我的UI端生成的图像尺寸是13.5 MB。有没有解码之前,我们必须执行它donwload功能。
(注意:下载是来自donwload.js库的功能。)

回答

0

找到解决方案。其实$ http服务默认情况下会改变对文本的响应。这使图像的大小加倍。为了避免它,我们需要添加以下参数。

的responseType: “斑点”

$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; 
 
});

相关问题