Nit*_*aul 5 blob npm typescript filesaver.js angular
我正在开发一个 angular 应用程序来下载用户详细信息,这些详细信息使用我的 angular 应用程序作为 word 文档上传到我的本地机器。
我能够成功上传它并将其保存到我的数据库中,并且我还能够在我的客户端结果中将其数据作为 byte[] 获取。
我正在使用npm i file-saver在我的 angular 应用程序中在客户端机器中执行保存。但是当我尝试这样做时,我在控制台中收到此错误
在 GET 调用 API 后,我的结果控制台输出如下所示
并使用此代码保存
saveAs(result, name+'_resume.pdf');
Run Code Online (Sandbox Code Playgroud)
我用result.blob试过了但仍然没有运气。有什么想法吗?
在我看到的一些帖子中
它刚刚从当前版本的 Chrome 中删除,那么我该如何克服呢?
更新
我做了两件事
我将打字稿代码更改为
this.dataservice.getResume(method, id).subscribe(blob => {
const file = new Blob([blob], { type: 'application/pdf' });
saveAs(file, name+'_resume.pdf');
Run Code Online (Sandbox Code Playgroud)
现在文件正在下载为 .pdf。但是当我尝试打开文件时,我无法加载错误:/
请看我的请求头
let headers = new HttpHeaders()
.set('Authorization', 'Bearer ' +
this.securityService
.securityObject.bearerToken);
return this.http.get(environment.baseApiUrl + methodName + id, { headers: headers , observe: 'response', responseType: 'blob' });
Run Code Online (Sandbox Code Playgroud)
代替
this.dataservice.getResume(method, id).subscribe(blob => {
const file = new Blob([blob], { type: 'application/pdf' });
saveAs(file, name+'_resume.pdf');
Run Code Online (Sandbox Code Playgroud)
和
this.dataservice.getResume(method, id).subscribe(blob => {
saveAs(blob.body, name+'_resume.pdf');
}
Run Code Online (Sandbox Code Playgroud)
对于接下来的步骤:
上面的代码仅适用于 pdf 文件,为了获取文件名,您可能需要在后端为响应标头添加一个标记(称为内容处置),并在客户端读取该标记
请参阅此处,了解有关内容处置以及您可能需要它的原因的更多信息
| 归档时间: |
|
| 查看次数: |
4739 次 |
| 最近记录: |