使用angular / http模块的角度文件损坏

时间:2018-09-04 16:00:40

标签: node.js angular amazon-s3

我正在使用文件保护程序和angular / http。上传,删除到s3存储桶效果很好,但是当我下载文件时显示损坏,文件大小很小(50B)

dowlnoad(attachment){
 var filename = attachment.uploadname;
 console.log(filename);
 this.service.downloadFile(filename)
 .subscribe(
    data => saveAs( new Blob([data], { type: 'application/octet-stream' 
    }),filename),
    error=> console.error(error)
)
 return false;

}

1 个答案:

答案 0 :(得分:-1)

downloadFile(file: string){
var body = {filename:file};

let headers = new Headers({ 'Content-Type': 'application/json', 'Accept': 'application/pdf' });
let options = new RequestOptions({ headers: headers, responseType: ResponseContentType.Blob });

 this.http.post(`${environment.queryApi}/api/dfile`, body, options).toPromise()
.then(response => this.saveToFileSystem(response,body));;
}

private saveToFileSystem(response,body) {
// const contentDispositionHeader: string = response.headers.get('Content-Disposition');
// const parts: string[] = contentDispositionHeader.split(';');
// const filename = parts[1].split('=')[1];
const blob = new Blob([response._body], { type: 'application/pdf' });
saveAs(blob, body.filename);
return false;
}

[response._body]解决了该问题