Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
274 views
in Technique[技术] by (71.8m points)

javascript - 从http发布请求下载文件-Angular 6(Download file from http post request - Angular 6)

UPDATED with res.send(data) instead of res.json(data)

(使用res.send(data)而不是res.json(data)更新)

Using Angular 6 and NodeJS I am doing a web application.

(使用Angular 6和NodeJS我正在做一个Web应用程序。)

I am trying to download a file from a http post request.

(我正在尝试从http发布请求下载文件。)

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.

(在组件中,我怀疑服务器的答案,当我有了它时,我用响应创建一个新的Blob,然后使用FileSaver下载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.

(现在,当我从服务器收到答案时,客户端将其视为错误,而状态为200。错误消息是: “在解析http:// localhost:3000 / api / experiment / regression期间Http失败”请参阅下面的屏幕截图。)

Component.ts

(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

(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.

(然后从服务器上,它使用发送的数据执行一些代码,并生成一个PDF文件。)

Then, I would like to send the pdf as a response to the client.

(然后,我想将pdf发送给客户。)

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:

(控制台日志为:)

控制台日志   ask by PierBJX translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Finally, I found a video tutorial and it was very basic.

(最后,我找到了一个视频教程,它非常基础。)

Node.js Server:

(Node.js服务器:)

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")
  });
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...