gpt4 book ai didi

node.js - Angular 5 - Node/Express - 无法下载 pdf

转载 作者:行者123 更新时间:2023-12-05 04:04:13 25 4
gpt4 key购买 nike

我正在尝试从结构类似的本地文件夹下载 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 确定。但在正常的浏览器控制台中显示如下附件

enter image description here enter image description here

注意:我从 here 中引用了 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;
}

关于node.js - Angular 5 - Node/Express - 无法下载 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52968015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com