node.js - Angular 5 - Node/Express - 无法下载 pdf
问题描述
我正在尝试从本地文件夹下载 pdf 文件,该文件夹的结构类似于 assets/test.pdf。
服务器.js
app.get('/ePoint', (req,res)=>{
// some dumb code :P
});
演示.ts
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Headers } from '@angular/http';
import {Observable} from 'rxjs';
fileDownload() {
const headers = new HttpHeaders();
headers.append('Accept', 'application/pdf');
this._http.get('http://localhost:3000/ePoint', { headers: headers })
.toPromise()
.then(response => this.saveItToClient(response));
}
private saveItToClient(response: any) {
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, filename);
}
我不知道我在哪里做错了。在浏览器网络控制台中。它的显示 200 好。但在普通浏览器控制台中显示如下附件
注意:我从这里提到了 ts 文件
非常感谢
解决方案
尝试这个...
组件.ts
downloadDocument(documentId: string) {
this.downloadDocumentSubscription = this.getService.downloadScannedDocument(documentId).subscribe(
data => {
this.createImageFromBlob(data);
},
error => {
console.log("no image found");
$("#errorModal").modal('show'); //show download err modal
});
}
createImageFromBlob(image: Blob) {
console.log("mylog", image);
if (window.navigator.msSaveOrOpenBlob) // IE10+
window.navigator.msSaveOrOpenBlob(image, "download." + (image.type.substr(image.type.lastIndexOf('/') + 1)));
else {
var url = window.URL.createObjectURL(image);
window.open(url);
}
}
服务.ts
downloadScannedDocument(documentId: string): Observable<any> {
let params = new HttpParams();
if (documentTypeParam == false)
params = params.set('onlyActive', 'false');
let fileResult: Observable<any> = this.http.get(`${this.apiBaseUrl}/${documentId}`, { responseType: "blob", params: params });
return fileResult;
}
推荐阅读
- elixir - 运行发行版的二进制文件时找不到 JSON 文件,但使用 `iex -S mix phx.server` 运行时可以找到它
- azure - 在 Azure DevOps 管道中,如何使用 addSpnToEnvironment 参数返回的服务原则详细信息登录 AZ CLI?
- firebase - 由于 Firebase 测试电话号码不起作用,应用商店拒绝了应用
- javascript - 谷歌饼图添加行 - 饼图未呈现
- rust - Rust 按位运算
- python - 改变元组内的元组
- react-native - 在使用 t.form 进行本机反应时使用 setState 更新数据
- java - 对角线和矩形之间的碰撞检测不正确
- javascript - 如何在 React intl 中创建新行?
- r - R中的多个if else语句