Ich arbeite an einer MEAN-Stack-App und ich versuche, eine PDF aus dem Backend (Express) im Frontend (Angular) anzuzeigen. Aber jedes Mal wird die Datei während der Übertragung beschädigt. Hat jemand eine Idee? Hier ist meine Codes (vereinfacht):Express Angular - Dienen PDF
Backend:
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);
}
Frontend:
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'})
))});
Edit: Middle:
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());
Hmm ... auch mit dieser Änderung, die Datei in der Ausgabe noch nicht lesbar ist. Ich habe die verwendeten Middlewares hinzugefügt, vielleicht ist einer von ihnen mit meiner Übertragung unordentlich? –