Node.js 用于缓冲pdf文件的NodeJS字符串
嗨,伙计们,我正试图下载一个pdf文件并保存在我的磁盘上。API向我发送一个字符串。但是下面的代码不起作用Node.js 用于缓冲pdf文件的NodeJS字符串,node.js,Node.js,嗨,伙计们,我正试图下载一个pdf文件并保存在我的磁盘上。API向我发送一个字符串。但是下面的代码不起作用 axios.get('https://myapi.com/download', config).then((res) => { var buff = Buffer.from(res.data, 'binary'); fs.writeFile('file.pdf', buff, function (err) { if (err) throw err; conso
axios.get('https://myapi.com/download', config).then((res) => {
var buff = Buffer.from(res.data, 'binary');
fs.writeFile('file.pdf', buff, function (err) {
if (err) throw err;
console.log('Saved!');
});
}).catch((e) => {
console.log(e);
})
我试过了,而且正在工作
fs.readFile('./download.pdf','binary', function (err, data) {
var str = data.toString();
var buff = Buffer.from(str, 'binary');
fs.writeFile('novopdf.pdf',buff, () => {
console.log('ok');
})
});
您需要按如下方式配置axios get请求
const response = await Axios({
method: 'GET',
url: url,
responseType: 'stream'
})
response.data.pipe(Fs.createWriteStream(path)) // path is location where you want to write the file.
然后检查响应对象上的结束事件。您能详细说明您面临的问题吗?当我从API保存pdf时,它保存了一个空文档。如果您对答案感到满意,请对答案进行投票。