3

I'm trying to upload a file (an image), the upload is fine, the file is stored in Mongo and have the same content type and same size as the original file, then when I try to download it, the file is corrupted but keeps the same content type (if I upload a pdf, it is recognized as a pdf, if it is a png, it is also recognized, but I can't open them). I don't understand what is wrong with this, it is pretty simple and standard.

For the upload from the client, I use angular ng-upload, for the download, it is a simple GET request to the route defined in the code.

EDIT : The file is well uploaded on the server, so the problem is when I try to read it from GridFS. The file that is downloaded is bigger than the one uploaded ! So the file isn't as the original and is corrupted, why ?

Here is my code.

//BACKEND

//ROUTES   
var multer  = require('multer');
var upload = multer({ dest: './tmp/'});
router.post('/:id/logo',upload.single('file'), uploadFile);
router.get('/:id/logo', getFile);

//Controller
var Grid = require('gridfs-stream');
var mongoose = require('mongoose');
var fs = require('fs');

uploadLogo = function(req, res) {
    var gfs = Grid(mongoose.connection.db, mongoose.mongo);
    var writeStream = gfs.createWriteStream();

    fs.createReadStream(req.file.path).pipe(writeStream);

    writeStream.on('close', function(file) {
        res.status(200).send({fileId: file._id});
    });
    writeStream.on('error', function(e) {
        res.status(500).send("Could not upload file");
    });
}

getFile = function(req, res) {

  var gfs = Grid(mongoose.connection.db, mongoose.mongo);

  // Check if the file exist
  gfs.findOne({ _id: req.params.id}, function(err, file) {
      if(err) {
          res.status(404).end();
      } else if(!file){
          res.status(404).end();
      } else {
          var readstream = gfs.createReadStream({
            _id: file._id
          });

          res.set('Content-Type', file.contentType);

          readstream.on('error', function (err) {
              res.send(500, err);
          });
          readstream.on('open', function () {
              readstream.pipe(res);
          });
      }
  });
}

//FRONT END
$scope.newFileUpload = function(file) {
  $scope.upload(file);
}

$scope.upload = function(file) {
  if (file && !angular.isUndefined(file.name)) {
    Upload.upload({
      url: 'api/' + $scope.myId + '/logo',
      fields: {
        'type': 'logo'
      },
      file: file
    }).success(function(data, status, headers, config) {
      $scope.imageId = data.fileId;
    }).error(function(data, status, headers, config) {
      console.log('file upload error status: ' + status);
    });
  }
};


//THE HTML
<div  class="drop-box"
        ngf-drop
        ngf-select
        ng-model="imageLogo"
        ngf-drag-over-class="dragover"
        ngf-allow-dir="true"
        accept="image/*"
        ngf-pattern="'image/*'"
        ngf-change="newFileUpload(imageLogo)"
        ngf-multiple="false"
        ngf-resize="{width: 200, height: 50}">
        Drop logo image here or click to upload</div>

        <input type="file" nv-file-select uploader="uploader"/>
Josip Ivic
  • 3,639
  • 9
  • 39
  • 57
Yanis26
  • 247
  • 2
  • 13
  • 1
    Did you find a solution ? I seem to be having the same problem. My downloaded file is bigger and also it is only corrupt for .pdf and .zip - .doc, .jpg and .mp3 work fine. – AbdealiLoKo Oct 27 '15 at 15:21
  • Same issue. Please me know if you find a solution. – Chinni Oct 27 '15 at 15:27
  • I couldn't find a solution, I asked on many forums and tried different way of doing it and it's always the same result. Now I just upload directly to AWS S3 and download from it, I don't use express or GridFS anymore. – Yanis26 Oct 27 '15 at 16:19

0 Answers0