Search code examples
node.jscorrupt

Corrupt image when downloading JPG from URL using NodeJS


I have created a function in JavaScript that downloads an image from a URL and writes the image to disk. The function works perfectly for most of the images I download, however there are 30 or so (out of 400) that are corrupted and will not open. The files are created but there is no extension associated and the details show that the file is 0 kB. I know that the files from the source are not corrupted because I have a working solution for this in C# which works for all files.

var download = function(url, destination, callback) {
    fs.exists(destination, function(exists){
        if (!exists){
            http.get(url, function(response) {
                var imageData = '';
                response.setEncoding('binary');
                response.on('data', function(chunk){
                    imageData += chunk;
                });
                response.on('end', function(){
                   fs.writeFile(destination, imageData, 'binary', function(error){
                      if(error){
                          console.log(error);
                      } else{
                          callback();
                      } 
                   });
                });
            });
        }
    });
};

I am working on a Windows machine, not sure if that could some how be an issue, so I figure I’d mention it.


Solution

  • So it turned out that I wasn't checking for illegal characters in the path string. In my C# solution I was using a different file name. Once I removed the illegal characters with regex, all is well.