37
42

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 でインターネット上の画像をダウンロード

Posted at

request で取得、fs で保存してみる

バイナリを壊さないように注意する。

  • request でのエンコーディング指定は {encoding: null}
  • fs でのエンコーディング指定は 'binary'
package.json
{
    "dependencies": {
        "request": "*",
        "fs": "*"
    }
}
test.js
var request = require('request');
var fs = require('fs');

var url = 'https://www.google.co.jp/images/nav_logo195.png';

request(
	{method: 'GET', url: url, encoding: null},
	function (error, response, body){
		if(!error && response.statusCode === 200){
			fs.writeFileSync('a.png', body, 'binary');
		}
	}
);
実行
npm install
node test.js

参考

37
42
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
37
42

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?