xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

How to use axios.js instead of request.js to get data as a buffer All In One

How to use axios.js instead of request.js to get data as a buffer All In One

如何使用 axios.js 代替 request.js 获取数据作为缓冲区

question

const fs = require("fs");
var path = require("path");
const { exit } = require("process");

// request
const request = require("request");
// const request = require("request-promise-native");

const log = console.log;

var folder = path.resolve(__dirname, '../pdf');
// log('folder', folder);
if (!fs.existsSync(folder)) {
  fs.mkdirSync(folder);
}

async function downloadPDF(url, filename) {
  log('🚧 pdf downloading ...');
  // ✅
  const pdfBuffer = await request.get({
    uri: url,
    encoding: null,
    // encoding: 'utf-8',
  });
  fs.writeFileSync(filename, pdfBuffer);
  log('✅ pdf finished!');
  // exit 0;
}

const url = 'https://cs193p.sites.stanford.edu/sites/g/files/sbiybj16636/files/media/file/l1.pdf';
const filename = folder + '/cs193p-2021-l1.pdf';

log('filename =', filename);
downloadPDF(url, filename);

error

const fs = require("fs");
var path = require("path");
const { exit } = require("process");

// axios
const axios = require("axios");

// const request = require("request");
// const request = require("request-promise-native");

const log = console.log;

var folder = path.resolve(__dirname, '../pdf');

// log('folder', folder);

if (!fs.existsSync(folder)) {
  fs.mkdirSync(folder);
}


async function downloadPDF(url, filename) {
  log('🚧 pdf downloading ...');
  axios({
    method: 'get',
    url,
    responseType: 'stream',
    // responseType: 'arraybuffer',
    // responseType: 'document',
  })
  .then((response) => {
    console.log(`response`, response.data)
    // ❌
    // response.data.pipe(fs.createWriteStream(filename))
    // response.data.pipe((pdfBuffer) => fs.writeFileSync(filename, pdfBuffer))
    let pdfBuffer = response.data;
    fs.writeFileSync(filename, pdfBuffer);
    // log('✅ pdf finished!');
  });
  // const pdfBuffer = await request.get({
  //   uri: url,
  //   encoding: null,
  //   // encoding: 'utf-8',
  // });
  // fs.writeFileSync(filename, pdfBuffer);
  // log('✅ pdf finished!');
  // exit 0;
}

const url = 'https://cs193p.sites.stanford.edu/sites/g/files/sbiybj16636/files/media/file/l1.pdf';
const filename = folder + '/cs193p-2021-l1.pdf';


log('filename =', filename);

downloadPDF(url, filename);


/* 

https://www.cnblogs.com/xgqfrms/p/16073509.html

https://www.npmjs.com/package/request ❌ This package has been deprecated

const request = require('request');
request('http://www.google.com', function (error, response, body) {
  console.error('error:', error); // Print the error if one occurred
  console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
  console.log('body:', body); // Print the HTML for the Google homepage.
});

https://www.npmjs.com/package/axios

// GET request for remote image in node.js
axios({
  method: 'get',
  url: 'https://bit.ly/2mTM3nY',
  responseType: 'stream'
})
.then(function (response) {
  response.data.pipe(fs.createWriteStream('ada_lovelace.jpg'))
});

*/

solution

  1. responseType: 'arraybuffer',

  2. Buffer.from(response.data, 'binary');


const fs = require("fs");
var path = require("path");
const { exit } = require("process");

// axios
const axios = require("axios");

const log = console.log;

var folder = path.resolve(__dirname, '../pdf');
// log('folder', folder);
if (!fs.existsSync(folder)) {
  fs.mkdirSync(folder);
}

async function downloadPDF(url, filename) {
  log('🚧 pdf downloading ...');
  axios({
    method: 'get',
    url,
    responseType: 'arraybuffer',
  })
  .then((response) => {
    console.log(`response`, response.data)
    // response <Buffer 25 50 44 46 2d 31 2e 34 0d 25 e2 e3 cf d3 0d 0a 35 35 20 30 20 6f 62 6a 0d 3c 3c 2f 4c 69 6e 65 61 72 69 7a 65 64 20 31 2f 4c 20 37 35 31 33 32 37 2f ... 751277 more bytes>
    // ✅
    let pdfBuffer = response.data;
    // ✅ 多此一举呀
    // let pdfBuffer = Buffer.from(response.data, 'binary');
    fs.writeFileSync(filename, pdfBuffer);
    log('✅ pdf finished!');
  });
}

const url = 'https://cs193p.sites.stanford.edu/sites/g/files/sbiybj16636/files/media/file/l1.pdf';
const filename = folder + '/cs193p-2021-l1.pdf';


log('filename =', filename);

downloadPDF(url, filename);

image

Buffer.from

Node.js

image

https://nodejs.org/api/buffer.html#buffer

demos

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

refs

https://stackoverflow.com/questions/41846669/download-an-image-using-axios-and-convert-it-to-base64



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2023-04-26 23:12  xgqfrms  阅读(21)  评论(0编辑  收藏  举报