Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 request to the server like this. From my component I call a function in a service. In the component, I susbscribe to have the answer of the server and when I have it I create a new Blob with the response and I Use FileSaver to download the pdf.

Now, when I received the answer from the server, the client sees it like an error whereas the status is 200. The error message is: "Http failure during parsing for http://localhost:3000/api/experiment/regression" See the screenshot below.

Component.ts

this.api.postML(this.regression).subscribe(
    res => {
      console.log(res);
      let pdf = new Blob(res.data, { type: "application/pdf" });
      let filename = "test.pdf";
      FileSaver.saveAs(pdf, filename);
    },
    err => {
      alert("Error to perform the regression");
      console.log(err);
    }
  );

API.Service.ts

  public postML(data): Observable<any> {
    // Create url
    let url = `${baseUrl}${"experiment/regression"}`;

    let options = {
      headers: { "Content-Type": "application/json", Accept: "application/pdf" }
    };
    // Call the http POST
    return this.http
      .post(url, data, options)
      .pipe(catchError(this.handleError));
  }

Then from the server, it executes some code with the data sent and generates a PDF file. Then, I would like to send the pdf as a response to the client. I tried like this:

fs.readFile("/home/user/test.pdf", function(err, data) {
  let pdfName = "Report.pdf";
  res.contentType("application/pdf");
  res.set("Content-Disposition", pdfName);
  res.set("Content-Transfer-Encoding", "binary");
  console.log(data);
  console.log("Send data");
  res.status(200);
  res.send(data);
});

In the client, I have the answer. The console log is: console log

like image 985
PierBJX Avatar asked Aug 10 '18 15:08

PierBJX


1 Answers

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:

public downloadReport(file): Observable<any> {
  // Create url
  let url = `${baseUrl}${"/experiment/resultML/downloadReport"}`;
  var body = { filename: file };

  return this.http.post(url, body, {
    responseType: "blob",
    headers: new HttpHeaders().append("Content-Type", "application/json")
  });
}
like image 149
PierBJX Avatar answered Nov 12 '22 10:11

PierBJX