2017-04-18 1 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
콘텐츠 유형 : application/octet-stream
날짜 : 1814 년 4 월 18 일 06:38:35 GMT
변경됨 : 원본
access-control-allow-credentials : true
위 코드는 정상적으로 작동합니다. 그러나 API에서 보낸 이미지의 크기는 7.5MB이고 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; 
 
});

관련 문제