【发布时间】:2018-02-19 19:09:27
【问题描述】:
我正在开发一个 MEAN 堆栈应用程序,我正在尝试在前端 (Angular) 中显示来自后端 (Express) 的 pdf。但是每次,文件在传输过程中都会损坏。有人有想法吗? 这是我的代码(简化):
后端:
import * as express from "express";
import * as fs from "fs";
myController.getPdf(req:express.Request, res:express.Response):void {
res.setHeader('Content-Type', 'application/pdf');
res.setHeader('Content-Length', fs.statSync('myPdf.pdf').size);
fs.createReadStream('myPdf.pdf).pipe(res);
}
前端:
import {Http,ResponseContentType} from "@angular/http";
constructor(@Inject(Http) private _http:Http) {}
this._http.get('ENDPOINT', {responseType: ResponseContentType.ArrayBuffer})
.map(response => {
window.open(URL.createObjectURL(
new Blob([response],{type:'application/pdf'})
))});
编辑:中间件:
import * as compression from "compression";
import * as zlib from "zbil";
import * as express from "express";
import * as bodyParser from "body-parser";
import * as session from "express-session";
import * as passport from "passport";
import * as morgan from "morgan";
import * as helmet from "helmet";
application: express.Application;
let _root = process.cwd();
application.use(compression({
level: zlib.Z_BEST__COMPRESSION,
threshold: "1kb"
}));
application.use(express.static(_root + "/node_modules/"));
application.use(express.static(_root + "/jspm_packages/"));
application.use(express.static(_root + "/client/dev/"));
application.use(bodyParser.json());
application.use(session({
secret: 'abcd',
saveUnitialized: true,
resave: true,
}));
application.use(passport.initialize());
application.use(passport.session());
application.use(morgan("dev"));
application.use(helmet());
【问题讨论】:
标签: node.js angular http express pdf