从http发布请求下载文件-Angular 6 [英] Download file from http post request - Angular 6

查看:552
本文介绍了从http发布请求下载文件-Angular 6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

使用Angular 6和NodeJS我正在做一个Web应用程序. 我正在尝试从http发布请求中下载文件.

Using Angular 6 and NodeJS I am doing a web application. I am trying to download a file from a http post request.

我这样向服务器发送请求.我从组件中调用服务中的函数.在该组件中,我怀疑服务器的答案,当我有了它时,我将使用响应创建一个新的Blob,然后使用FileSaver下载pdf.

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.

现在,当我从服务器收到答案时,客户端将其视为错误,而状态为200.错误消息为: "http://localhost:3000/api/experiment/regression " 请参见下面的屏幕截图.

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

然后从服务器上,它使用发送的数据执行一些代码,并生成一个PDF文件. 然后,我想将pdf发送给客户. 我这样尝试过:

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:

推荐答案

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

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

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

角度组件:

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

角度服务:

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

这篇关于从http发布请求下载文件-Angular 6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆