Skip to content

Instantly share code, notes, and snippets.

@dogancelik
Last active November 5, 2020 01:37
Show Gist options
  • Save dogancelik/fb8cf8a61877070b0f70 to your computer and use it in GitHub Desktop.
Save dogancelik/fb8cf8a61877070b0f70 to your computer and use it in GitHub Desktop.
How to download an image file? (Example) #Node.js
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')
npm install request unirest needle bhttp
@swooningnico26
Copy link

it didn't work.. it says unexpected token {
request.get {url: url, encoding: 'binary'}, (err, res) -> <-- this line

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment