Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

incomplete / partial Images are downloaded using node js

I am downloading images from a site using node js in a standard way.

var download = function (uri, filename, callback) {
    request.head(uri, function (err, res, body) {
        console.log('content-type:', res.headers['content-type']);
        console.log('content-length:', res.headers['content-length']);

        request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
    });
};

But when I run this code in for loop, the images saved are corrupt. More to say, all the pixels are not downloaded. Only partial image is saved.

Is there a way to fix this issue?

like image 806
Manish Avatar asked Aug 09 '26 00:08

Manish


1 Answers

Well, I found the solution to the problem. The reason was that before the image is completely written onto the disk, another thread starts and so on. To fix this, I implemented the promises using Q-IO library.

var Apps = require("q-io/http");
var qfs = require("q-io/fs");

var download = function(uri, filename, callback){
Apps.read(uri).then(function(data){
    callback(data);
}, function(err){
    console.log('err: ' + err);
});
};

download('https://www.google.com/images/srpr/logo3w.png', 'test.jpg', function(data){
    console.log('done');
    qfs.write("test.jpg", data);
});

But instead of using for loop, I used self invoking function.

like image 155
Manish Avatar answered Aug 11 '26 13:08

Manish



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!