由Spring Rest Service下载的Angular 4 Application下载的文件已损坏

时间:2018-03-19 17:54:16

标签: node.js angular http-post

我们有一个用Spring编写的Post rest服务。这个服务的响应是一个json,它包含一些字符串参数和一个字节数组。字节数组可以用于任何类型的文件。

此服务将由Angular 4中的Angular应用程序使用。 在Angular页面上,我们想要点击一个按钮下载该文件。下面的代码访问其余服务,获取正确的响应数据,但它下载的文件已损坏。

我已经拿了一个jpg文件作为示例,在尝试打开下载的文件时,我收到错误"我们无法打开此文件"。我们已经验证了响应中收到的字节数组是否完整。

bmsService.service.ts

viewDocument(inputdata: String) {
console.log(inputdata);
this.HEADERS.append('Content-Type', 'application/json');
return this.http.post(
    VIEW_DOCUMENT_URL, 
    inputdata, 
    {headers:this.HEADERS}
 ).map((res: Response) => res.json());
}

app.component.ts

openViewPopUp(docId: String) {

let attachmentRequest = new AttachmentRequest();
attachmentRequest.docId = docId;
this.bmsService.viewDocument(JSON.stringify(attachmentRequest)).subscribe(
  response => {
    console.log(response);
    let contentType = 'application/octet-stream';
    let blob = new Blob([response.fileBytes], { type: contentType });
    let link=document.createElement('a');
    link.href=window.URL.createObjectURL(blob);
    link.download="Case5_Evidence.jpg";
    link.click();
  },
  err => {
    console.log(err);
  });
}

单击按钮时调用openViewPopUp()方法。正在使用的浏览器是IE11。

0 个答案:

没有答案