Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

149
Views
Javascript : Image not downloading anymore

The code bellow used to download an image while adding a custom name to it. I kinda forgot about it for some time and wanted to retry the download and it doesn't work anymore. Did I messed up anything here ?

let imageInit =
  'https://via.placeholder.com/350x150';
let image = imageInit.replace('350x250', '200x200');
if (!image) {
  return;
}

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);
  });
};

const generatedName = uuidv4();
const finalName = generatedName.concat('.png');

download(image, finalName, function () {
  console.log('done');
});

Edit : This is weird. When trying with this link 'https://www.google.com/images/srpr/logo3w.png' it downloads, but doesn't work with any other link.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

When I fill in the appropriate modules that you're using, this code here works just fine for me in node v16.13.2:

const request = require('request');
const { v4: uuidv4 } = require('uuid');
const fs = require('fs');

let imageInit = 'https://via.placeholder.com/350x150';
let image = imageInit.replace('350x250', '200x200');
if (!image) {
    return;
}

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);
    });
};

const generatedName = uuidv4();
const finalName = generatedName.concat('.png');
console.log(finalName);

download(image, finalName, function() {
    console.log('done');
});

Is there something different in your whole code? Or does this specific code not work for you?

Things you can examine in your own code:

  1. Add error handling to both request() stream and to the writeStream to log any errors.
  2. Try to load the https://via.placeholder.com/200x200 URL into your own browser to just see if that works.
  3. Tell us more about "doesn't work anymore" actually means. What do you observe? What do you see in the logs? Where does it stop executing?

Here's a version of the code with a lot more error handling/logging:

const request = require('request');
const { v4: uuidv4 } = require('uuid');
const fs = require('fs');

let imageInit = 'https://via.placeholder.com/350x150';
let image = imageInit.replace('350x250', '200x200');
if (!image) {
    return;
}

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

        let wStream = fs.createWriteStream(filename).on('error', err => { console.log(err); });
        let rStream = request(uri).on('error', err => { console.log(err); });
        rStream.pipe(wStream).on('close', () => callback(null));
    });
};

const generatedName = uuidv4();
const finalName = generatedName.concat('.png');
console.log(finalName);

download(image, finalName, function(err) {
    if (err) {
        console.log('finished with error');
    } else {
        console.log('done');
    }
});

Please note that the request() module has been deprecated so really shouldn't be used in new code going forward. A good set of alternatives here.

about 4 years ago · Juan Pablo Isaza Report

0

After a lot of research I found the source of the problem; I had process.exit(1); in the end of my code.

This was added in order to end the execution of the whole code once everything is finished, but apparently with this the image is not downloaded too. Removing it fixed the issue in addition of the snippet provided by @jfriend00.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!