首页 > 解决方案 > 如何使用 axios 从 nodejs 接收文件

问题描述

我需要 axios 从 nodejs 获取 pdf 缓冲区以及更多一些数据信息。

如果我使用 responseType:"arraybuffer" 设置 axios 帖子,并且我让节点只发送它正在工作的 pdf 缓冲区。

但我需要从节点返回更多数据信息,如果我摆脱“responseType:arrayBuffer”,为了接收 json,我无法使用新的 Blob 函数将 pdfBuffer 转换为 pdf。我得到 BLOB 无效。

我究竟做错了什么?

这不起作用:

//前端:

const resp=await axios.post("atestadosClientes/generatePDF", data);

 const file = new Blob([response.data.pdfBuffer], {
          type: "application/pdf",
        });
//Build a URL from the file
const fileURL = URL.createObjectURL(file);

//Node response:generate pdf buffer
return res.send({
      message: "Success",
      id: resp[0].insertId,
      pdfBuffer: pdfSignedBuffer, //pdf buffer
    });

这是有效的:

//前端:

const resp=await axios.post("atestadosClientes/generatePDF", data, {
      responseType: "arraybuffer",
    });
  },
const file = new Blob([response.data], {
          type: "application/pdf",
        });
//Build a URL from the file
const fileURL = URL.createObjectURL(file);

//节点响应:生成pdf缓冲区

return res.send(pdfBuffer)

标签: javascriptnode.jsaxios

解决方案


推荐阅读