Download File From Http Post Request - Angular 6
UPDATED with res.send(data) instead of res.json(data) Using Angular 6 and NodeJS I am doing a web application. I am trying to download a file from a http post request. I send a req
Solution 1:
Finally, I found a video tutorial and it was very basic.
Node.js Server:
const express = require("express");
const router = express.Router();
router.post("/experiment/resultML/downloadReport",downloadReport);
const downloadReport = function(req, res) {
res.sendFile(req.body.filename);
};
Angular Component:
import { saveAs } from"file-saver";
...
download() {
let filename = "/Path/to/your/report.pdf";
this.api.downloadReport(filename).subscribe(
data => {
saveAs(data, filename);
},
err => {
alert("Problem while downloading the file.");
console.error(err);
}
);
}
Angular Service:
publicdownloadReport(file): Observable<any> {
// Create urllet url = `${baseUrl}${"/experiment/resultML/downloadReport"}`;
var body = { filename: file };
returnthis.http.post(url, body, {
responseType: "blob",
headers: newHttpHeaders().append("Content-Type", "application/json")
});
}
Post a Comment for "Download File From Http Post Request - Angular 6"