I have been struggling to succeed in downloading an image without piping it to fs. Here's what I have accomplished:
var Promise = require('bluebird'), fs = Promise.promisifyAll(require('fs')), requestAsync = Promise.promisify(require('request')); function downloadImage(uri, filename){ return requestAsync(uri) .spread(function (response, body) { if (response.statusCode != 200) return Promise.resolve(); return fs.writeFileAsync(filename, body); }) .then(function () { ... }) // ... } A valid input might be:
downloadImage('http://treasure.diylol.com/uploads/post/image/301917/resized_business-cat-meme-generator-thanks-for-all-your-help-see-you-around-soon-f82a01.jpg', 'c:\\thanks.jpg'); I do believe the problem is with the handling of body. I have tried casting it to a Buffer (new Buffer(body, 'binary') etc.) in several encodings, but all failed.
Thanks from ahead for any help!