Javascript 如何使用Sails JS处理可选文件上传?

Javascript 如何使用Sails JS处理可选文件上传?,javascript,node.js,forms,file-upload,sails.js,Javascript,Node.js,Forms,File Upload,Sails.js,我有一个表单,用户可以提交或不提交文件 表格: <form method="post" action="/file/upload" enctype="multipart/form-data"> <input type="file" name="media" /> <input type="submit" value="Submit" /> </form> 如果他们不上传文件,我会得到以下错误。除非我进入skipper代码并注释掉抛

我有一个表单,用户可以提交或不提交文件

表格:

<form method="post" action="/file/upload" enctype="multipart/form-data">
    <input type="file" name="media" />
    <input type="submit" value="Submit" />
</form>
如果他们不上传文件,我会得到以下错误。除非我进入skipper代码并注释掉抛出的错误,否则似乎没有办法捕获或抑制它如何在不附加文件的情况下提交表单而不使应用程序崩溃?

Error: EMAXBUFFER: An Upstream (`NOOP_media`) timed out before it was plugged into a receiver. It was still unused after waiting 4500ms. You can configure this timeout by changing the `maxTimeToBuffer` option.

我已经浏览了很多论坛和博客帖子,但到目前为止没有任何帮助。

你必须在
.upload()中进行检查。删除
if
语句

req.file('media').upload({
  dirname: '/tmp/uploads'
}, function whenDone(err, uploadedFiles) {
  if(uploadedFiles.length === 0){ // Check the number of files uploaded.
    return res.send('no file given!');
  }
  if (err) {
    sails.log.error('Error uploading file', err);
  }
  return res.send('thanks for your file');
});

您必须在
.upload()
中进行检查。删除
if
语句

req.file('media').upload({
  dirname: '/tmp/uploads'
}, function whenDone(err, uploadedFiles) {
  if(uploadedFiles.length === 0){ // Check the number of files uploaded.
    return res.send('no file given!');
  }
  if (err) {
    sails.log.error('Error uploading file', err);
  }
  return res.send('thanks for your file');
});

当一个文件输入为空时,它看起来像是一个普通的文本输入,它将为空,因此您可以尝试检查以下内容:

 if(typeof req.param('media') !== 'undefined' && req.param('media').length == 0)) {
        return res.send('no file given!');
   }else {  //handle the file upload }

当一个文件输入为空时,它看起来像是一个普通的文本输入,它将为空,因此您可以尝试检查以下内容:

 if(typeof req.param('media') !== 'undefined' && req.param('media').length == 0)) {
        return res.send('no file given!');
   }else {  //handle the file upload }

您可以使用
noMoreFiles()
中止流

const skipperUpstream=req.file('media');
//skipperUpstream.\u files是一个内部数组,包含密钥`介质的上载文件`
//这里我只希望有一个文件,或者没有
const file=skipperUpstream._文件[0];
如果(!文件){
//'skipperUpstream.\uu proto_uuu'是'Upstream'。它提供'noMoreFiles()'来停止接收文件。
//它还清除所有超时:https://npmdoc.github.io/node-npmdoc-skipper/build/apidoc.html#apidoc.element.skipper.Upstream.prototype.noMoreFiles
skipperUpstream.noMoreFiles();
返回;
}
skipperUpstream.upload(/*你的东西*/);

您可以使用
noMoreFiles()
中止流

const skipperUpstream=req.file('media');
//skipperUpstream.\u files是一个内部数组,包含密钥`介质的上载文件`
//这里我只希望有一个文件,或者没有
const file=skipperUpstream._文件[0];
如果(!文件){
//'skipperUpstream.\uu proto_uuu'是'Upstream'。它提供'noMoreFiles()'来停止接收文件。
//它还清除所有超时:https://npmdoc.github.io/node-npmdoc-skipper/build/apidoc.html#apidoc.element.skipper.Upstream.prototype.noMoreFiles
skipperUpstream.noMoreFiles();
返回;
}
skipperUpstream.upload(/*你的东西*/);