0

When uploading files with Sails JS the server crashes if there is no file sent with the request, considering this to be the controller action:

function(req, res) {
  req.file('testFile').upload(function() {
    // do something...
  });
}

I have tried to check the headers, but there seems to be no difference between a file being sent or not.

I am looking for something like this:

function(req, res) {
  if(file sent) {
    req.file('testFile').upload(...);
  } else {
    // file was not sent, do something else
  }
}

Is there a way that I could achieve this behavior of uploading a file or not on the same API?

4

2 回答 2

0

我想这就是你要找的

req.file('testFile').upload(function (err, uploadedFiles){
  if (err) return res.send(500, err);

  return res.send(200, uploadedFiles);
});
于 2014-12-24T19:04:23.340 回答
0

这并不完全回答您的问题,但至少它是一个解决方案:

req.file('testFile').upload(function (err, uploadedFiles){
   if (err) return res.send(500, err);
   if (uploadedFiles.length < 1) {
      return res.badRequest('file missing or could not be uploaded');
   }
   return res.send(200, uploadedFiles);
 });

UploadFiles 包含一组成功上传的文件,如果它们可用。

于 2015-05-27T09:07:15.767 回答