【问题标题】:File is corrupt in angular using angular/http module使用 angular/http 模块的角度文件损坏
【发布时间】:2018-09-05 04:14:04
【问题描述】:

我正在使用文件保护程序和 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;

}

【问题讨论】:

  • 是报错还是成功?什么是 saveAs 功能?你可能需要this.saveAs(...
  • 它没有点击错误消息,文件正在下载但内容不完整。我试过 this.save 因为它不起作用。
  • saveAs 是从 file-saver 导入的

标签: node.js angular amazon-s3


【解决方案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] 解决了问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-08
    • 2017-12-03
    • 2017-12-20
    • 2014-02-25
    • 1970-01-01
    • 2021-10-23
    • 2018-09-17
    • 1970-01-01
    相关资源
    最近更新 更多