javascript - 如何使用 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)
解决方案
推荐阅读
- matlab - 相同代码的两个版本返回不同的结果
- jquery - 新材料设计图标主题 - 设置颜色
- pyspark - 将科学字符串转换为在 pyspark 2.4 中浮动会产生舍入错误
- phpstorm - PhpStorm:如何使用单引号代替双引号进行自动导入?
- java - PoolablePreparedStatement 内存泄漏
- git - 仅在添加新标签时才在 post-receive 中触发某些内容
- python - 循环遍历 DataFrame 以保存手动重复的任务
- c - 仅在中断期间读取变量时需要 volatile
- excel - 随机 Excel 崩溃 xlsm
- google-sheets - 如果没有值,如何突出显示特定范围的单元格