21
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Node.jsで画像をダウンロードして保存する(axios利用)

Last updated at Posted at 2018-01-17

axiosを使うとPOSTリクエストなどでデータの送信をする場面が多いですが、今回は画像のダウンロードをしてみます。

今日、ジーズアカデミーで授業して来ましたが、調べるとSOFのページ(Download an image using Axios and convert it to base64)などが出てきてあまり記事が無いのでメモです。

千石電商さんのページからNefry BTの画像をDLして保存してみます。

$ npm i --save axios
app.js
'use strcit';

const axios = require('axios');
const BASE_URL = `http://sengoku.co.jp/item/images/th230/nefry_BT_.jpg`;
const fs = require('fs');

const main = async () => {
	const res = await axios.get(BASE_URL, {responseType: 'arraybuffer'});
    fs.writeFileSync(`./nefry.jpg`, new Buffer.from(res.data), 'binary');
    console.log(`done`);
};

main();

これで保存できます。

21
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?