Last active
November 5, 2020 01:37
-
-
Save dogancelik/fb8cf8a61877070b0f70 to your computer and use it in GitHub Desktop.
How to download an image file? (Example) #Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
request = require 'request' | |
http = require 'http' | |
fs = require 'fs' | |
unirest = require 'unirest' | |
needle = require 'needle' | |
bhttp = require 'bhttp' | |
url = 'http://raw.githubusercontent.com/alrra/browser-logos/master/main-desktop.png' | |
# request | |
request.get {url: url, encoding: 'binary'}, (err, res) -> | |
if not err and res.statusCode is 200 | |
fs.writeFile('file1.png', res.body, encoding: 'binary') | |
# http | |
http.get url, (res) -> | |
if res.statusCode is 200 | |
res.pipe(fs.createWriteStream('file2.png')) | |
# unireq | |
unireq = unirest.get(url) | |
unireq.options['encoding'] = 'binary' | |
unireq.end (res) -> | |
if res.statusCode is 200 | |
fs.writeFile('file3.png', res.body, encoding: 'binary') | |
# needle | |
needle.get url, (err, res) -> | |
if not err and res.statusCode is 200 | |
fs.writeFile('file4.png', res.raw) | |
# bhttp | |
bhttp.get(url, stream: true).then (response) -> | |
if response.statusCode is 200 | |
response.pipe fs.createWriteStream('file5.png') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
npm install request unirest needle bhttp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it didn't work.. it says unexpected token {
request.get {url: url, encoding: 'binary'}, (err, res) -> <-- this line