title: File download giving corrupt file in Nodejs
body: <p>I am using request module in NodeJS to read data from AWS S3. When I am downloading the file(docx or image or pdf) using below code,its giving me an invalid/corrupted file. But when I am downloading .txt file it's not getting corrupted and I am able to see file in notepad.</p>
  
  <p>I did a bit of googling and as suggested also tried by setting encoding to binary, still its not giving required result.</p>
  
  <p>File upload is working fine. And I am able to see the uploaded file in AWS console.</p>
  
  <p>File download code</p>
  
  <pre><code>var s3 = new properties.AWS.S3();
      var params = {Bucket: properties.AWS_BUCKET, Key: req.headers['x-org'] + "/" + "talk" + "/" + req.body.fileName};
      s3.getSignedUrl('getObject', params, function (err, URL) {
          if (err) {
              console.log("Error inside the S3");
              console.log(err, err.stack); // an error occurred
              res.send(null);
          } else {
              console.log("After getObject:-" + URL);
              request({
                  url: URL, //URL to hit
                  method: 'GET',
                  encoding: 'binary'
              }, function (error, response, body) {
                  if (error) {
                      console.log(error);
                  } else {
                      //console.log(response.statusCode, body);
                      res.set('content-disposition', 'attachment; filename=' + req.body.fileName);
                      res.send(body);
                  }
              });
          }
      });
  </code></pre>
  
  <p><strong>Update</strong>:-
  I have narrow down the error, and just trying to send the file by reading file from local file system. Even that also is giving the corrupted files on client.
  Here's the code for same</p>
  
  <pre><code>var filePath = path.join(__dirname, '..', '..', '..', 'downloads', req.body.fileURL);
      var stat = fs.statSync(filePath);
      var filename = path.basename(filePath);
      var mimetype = mime.lookup(filePath);
      console.log("mimetype=" + mimetype);
  
      res.setHeader('Content-disposition', 'attachment; filename=' + filename);
      res.setHeader('Content-type', mimetype + ";charset=UTF-8");
      res.setHeader('Content-Length', stat.size);
      var filestream = fs.createReadStream(filePath);
      filestream.pipe(res);
  </code></pre>