1

I'm trying to make it possible to upload an excel file on a web application. But when I upload my file, there is an inifnite loop on the web application but the file is correctly uploaded, here is the code :

UPDATE

Node :

var storage = multer.diskStorage({
  destination: function(req,file,cb) {
    cb(null,'/uploads/')
  },
  filename: function(req,file,cb) {
    cb(null, file.originalname);
  }
});

var upload = multer({ storage : storage });

app.post('/upload', upload.single('file'), function(req,res) {
  var tmp_path = req.file.path;
  var target_path = './uploads/'+req.file.originalname;
  var source = fs.createReadStream(tmp_path);
  var dest = fs.createWriteStream(target_path);
  var stream = source.pipe(dest);
  stream.on('finish', function() {
     res.send();
    });
});

Angular :

$scope.uploadFile = function() {
  var file = $scope.myFile;
  var uploadUrl = "/upload";
  var fd = new FormData();
  fd.append('file',file);
}

$http.post(uploadUrl,fd,{
     headers: {'Content-Type' : 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'}
});

With the solution that has been proposed by Pierre Emmanuel Lallemant, there is no more infinite loop, but the file is corrupted after the upload, its size is 0 Ko instead of 850 Ko.

Tewan
  • 171
  • 1
  • 12
  • You have to send a response in the '/upload' callback . call `res.send() ` once the file is saved. If you don't, expressjs thinks your `/upload` call hasn't finished its job, so the application is blocked. – Pierre Emmanuel Lallemant Jun 10 '17 at 19:42
  • @PierreEmmanuelLallemant How do I detect the file is saved ? – Tewan Jun 10 '17 at 19:49

2 Answers2

0

To detect if the file is saved, the following code should work:

dest.on('finish', function() {  
  res.send();
});

And this one must work:

source.pipe(dest).on('finish', function() {  
  res.send();
});

callback to handle completion of pipe

0

I'm using multer, so I didn't to code all the process.

I've just to do :

app.post('/upload', upload.single('propt'), function (req, res) {
    res.send();
});
Tewan
  • 171
  • 1
  • 12