1

im trying to fetch an image through url in nodejs ( using node-fetch ), something like this

fetch(url)
    .then(async res => {
        const dest = fs.createWriteStream(filename);
        res.body.pipe(dest)
    })
    .catch((err) => {
        console.log(err)
    })

the thing is i dont know how to check if the image is loaded properly with this method or not and of course the image will be broken like this

enter image description here

Any suggestion guys? Thanks for reading

  • You can properly listen for all possible errors in the request/download in case the request is interrupted midstream or runs into some error midstream. Other than that, the only way to know whether you have a legal image would be to find some library that verifies that the image is all in the proper format, but if the server gave you a legal image with junk data, there's no way for you to know that. – jfriend00 Jan 27 '21 at 06:50

0 Answers0