node.js - 在nodejs中将二进制转换为base64
问题描述
有一个直接下载链接,可以根据 GET 请求下载 PDF。当我使用像邮递员这样的 REST 客户端发出获取请求时,响应中会收到二进制数据。我想将此数据转换为base64。现在,这个 base64 字符串需要传递给其他 API,后者又将文件发送到 WhatsApp 聊天。我在 WhatsApp 中成功收到了文件,但它完全是空白的。文件大小与源文件相同,页数也相同。
var axios = require('axios');
let base64;
var config = {
method: 'get',
encoding: null,
url: '.................',
};
axios(config)
.then(function (response) {
// console.log(response.data, "-----------------------------------\n");
console.log(typeof response.data);
// base64 = Buffer.from(response.data).toString('base64');
// base64 = Buffer.from(response.data).toString('base64');
// base64 = new Buffer(response.data, "binary");
// base64 = Buffer.concat(response.data);
// base64 = new Buffer(response.data, "base64");
base64 = response.data.toString('base64');
var data = {...........};
var config2 = {
method: 'post',
url: '...........',
headers: {
'Content-Type': 'application/json',
'x-auth-token': '...........',
'Host': '.............'
},
data : data
};
axios(config2)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log("error while sending");
});
})
.catch(function (error) {
console.log(error, "error while requesting for the file");
});
我尝试了以下解决方案,但没有一个对我有用。
解决方案
推荐阅读
- python - Ruamel yaml 将 yaml 转换为 jinja2 模板,没有不需要的引号
- python - Pycharm 包括 \n 与多行输入()
- database - Mongo 转储和恢复数据库
- css - 在 Firefox 中将具有“纵横比”的子 div 适合父级
- javascript - 在反应中断开连接后重新连接到套接字通道
- java - SpringBoot - 不良记录实践:系统输出流的使用
- android - android是否可以通过BLE丢失通过蓝牙写入特征的命令的一部分?
- node.js - 引擎“节点”与此模块不兼容。预期版本“XXX”。得到“XXX”
- c - flex中字符类的负范围
- docusaurus - 如何从组件库修改博客的 Docusaurus 外观?