Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 用于缓冲pdf文件的NodeJS字符串_Node.js - Fatal编程技术网

Node.js 用于缓冲pdf文件的NodeJS字符串

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

嗨,伙计们,我正试图下载一个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;
    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时,它保存了一个空文档。如果您对答案感到满意,请对答案进行投票。