나는 평균 스택 응용 프로그램에서 작업 중이며 프론트 엔드 (각도)에 백엔드 (익스프레스)에서 pdf를 표시하려고합니다. 그러나 매번 파일은 전송 중에 손상됩니다. 누구나 아이디어가 있습니까? 은 여기 내 코드 (간체) :Express Angular - Serve 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());
흠은 ..., 파일이 여전히 출력에서 읽을 수 없습니다. 사용 된 미들웨어를 추가했습니다. 아마도 그 중 하나가 전송을 망칠 것입니까? –