简体   繁体   English

Sails JS检查是否发送了上传文件

[英]Sails JS check if upload file is sent

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: 使用Sails JS上载文件时,如果没有随请求发送的文件,则服务器崩溃,将其视为控制器操作:

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? 有没有一种方法可以实现在相同的API上上传文件或不上传文件的行为?

I think this is what you're looking for 我认为这就是您要寻找的

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

  return res.send(200, uploadedFiles);
});

This does not exactly anwser your question, but at least it is a solution: 这并不能完全解决您的问题,但至少是一个解决方案:

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);
 });

uploadedFiles contains an array of successfully uploaded files, it they were available. uploadFiles包含一组成功上传的文件,这些文件可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM