2017-03-14 13 views
0

내가, 내 백엔드 작품을 추측 실패는 각도 2 : 덩어리로 우편 번호를 다운로드하는 것은이 작품과 지퍼 미세하기 때문에

curl -X POST -H 'Content-Type: application/json' -d '{}' http://localhost:3000/zip/create > file.zip 

내 장고 백엔드 반환이 같은 데이터 :

return HttpResponse(data, content_type='application/octet-stream') 

ZIP이 도착하면 어떤 식 으로든 손상되어 종료 할 수 있기 때문에 Angular 2 코드에서 무엇이 잘못되었는지 알 수 없습니다. 이 우편을 다운로드하는 URL을 클릭하면

let blob = new Blob([response._body], { "type": 'application/octet-stream;' }); 

this.exportUrl = this.sanitizer.bypassSecurityTrustUrl(URL.createObjectURL(blob)); 

this.exportFileName = "download.zip"; 

을했지만 작동하지 않습니다

실제 데이터는 response._body 것 같다. 그래서 분명히 어떻게 든 바이너리 데이터를 잘못 처리합니까?

내 서비스는 기본적으로 이것이다 :

return this.http.post(
    this.zipUrl, 
    JSON.stringify(zipCreationParameters), 
    {headers: {'Content-Type': 'application/json'} }); 

답변

2

내가 이렇게 내 서비스를 수정하여이 작업을 얻었다 :

return this.http.post(
    this.createUrl, 
    JSON.stringify(data), 
    {headers: this.headers, responseType: ResponseContentType.Blob}); 

추가 ResponseContentType.Blob는 내용이 덩어리로 표시하게하고 일했다!

0

여러분이 고려해야 할 중요한 사항은 다음과 같습니다. in backend UTF-8 컨텐츠를 Base64로 인코딩 한 다음 produce = "application/zip"으로 보내주십시오. 프론트 엔드 atob 함수가 base64는 지원하지만 utf는 지원하지 않기 때문에

당신이 uI response._body에서받는 것은 bytestring이기 때문에. 이제 decode (atob (귀하의 콘텐츠))로 을 디코딩하십시오. 그 후에 각 bytecharacter를 가져 와서 Uint8Array에 넣으십시오. 그거야. 그것은 연구의 1 일 후에 나를 위해 완벽하게 작동합니다.

function convertDataURIToBinary(dataURI) { 
    var base64Index = dataURI.indexOf(BASE64_MARKER) + BASE64_MARKER.length; 
    var base64 = dataURI.substring(base64Index); 
    var raw = window.atob(base64); 
    var rawLength = raw.length; 
    var array = new Uint8Array(new ArrayBuffer(rawLength)); 

    for(i = 0; i < rawLength; i++) { 
    array[i] = raw.charCodeAt(i); 
    } 
    return array; 
}