2016-08-24 3 views
8

서버에서 생성 된 PDF를 Angular 2 RC 5 프로젝트의보기로 표시하려고합니다. 현재 ASPNETCORE 서버는 개체를 'application/pdf'로 반환하고 Angular 클라이언트는 응답을 blob로 구문 분석하려고합니다.각도 2 RC 5 서버에서 PDF 가져 오기 및 표시 시도

Error: The request body isn't either a blob or an array buffer

나는 PDF 서버 전화를 사용하고 코드는 본질적으로 : BLOB하는 responseType을 설정하는

getHeaders() : Headers { 
    var headers = new Headers({ 
     'responseType': 'application/blob' 
    }); 
    return headers; 
} 

getBlob() { 
    return this.http.get(uri, new RequestOptions({headers: this.getHeaders()}, body: "" })) 
    .map(response => (<Response>response).blob()); 
} 

답변

17

봅니다 그러나, 나는 클라이언트 측에서 다음과 같은 오류가 , 그것을 작동합니다 :

getBlob() { 
return this.http.get(uri, {responseType: ResponseContentType.Blob}) 
.map(response => (<Response>response).blob()); 

을}

+1

을 즐길 수 있습니다. RC5에서 작동 – reflog

+0

저장된 파일을 열려고하면 데이터가 누락되면 오류가 발생합니다. –

+0

이렇게하면 요청을 보내고 응답을 받지만 응답 텍스트를 가져올 명확한 방법이없는 Blob로 응답을 반환합니다. – ShadSterling

1

일의 F를 나 나 :

구성 요소 :

downloadInvoice(invoice) { 
    this.loading = true; 
    this.invoiceDataService 
    .downloadInvoice(invoice) 
    .subscribe(
    (blob) => { 
     FileSaver.saveAs(blob, 'test.pdf'); 
    }, 
    error => this.error = error, 
    () => { 
     this.loading = false; 
     console.log('downloadInvoices : Request Complete') 
    } 
) 
} 

데이터 서비스 :

downloadInvoice(invoice): Observable<Blob> { 
    return this.api.downloadInvoice(invoice); 
} 

API 서비스 :

downloadInvoice(invoice: Invoice): Observable<Blob> { 
    return this.authHttp 
    .get(this.apiBase + '/invoices/' + invoice.hashid + '/download', {responseType: ResponseContentType.Blob}) 
    .map(response => { 
    return new Blob([response.blob()], {type: 'application/pdf'}); 
    }) 
    .catch(this.handleError.bind(this)); 
} 

이는 '현대'답변입니다 :)