title: Corrupt image when downloading JPG from URL using NodeJS
body: <p>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.</p>
  
  <pre><code>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();
                        } 
                     });
                  });
              });
          }
      });
  };
  </code></pre>
  
  <p>I am working on a Windows machine, not sure if that could some how be an issue, so I figure I’d mention it.</p>