在sails.js中上传多个文件

时间:2018-11-17 09:11:31

标签: javascript html node.js sails.js waterline

我正在尝试在Sails框架中上传多个文件。下面是代码

req.file('photos').upload(function (err, uploadedPhotos) {
  if (err) return res.serverError(err);
  async.eachSeries(uploadedPhotos, 
    function (uploadedFile, callback) {
      sails.log("upload file size and name is " + uploadedFile.size + " " + uploadedFile.filename);

      req.file(uploadedFile).upload({
        dirname: "path/to/directory",
        // saveAs: function (file, cb) { cb(null, file.filename); }
      },
      function onUploadComplete(err, files) {
          if (err) return res.serverError(err);

          if (files.length === 0) {
            return res.badRequest('No file was uploaded');
          }
          sails.log("files uploaded");
      });
    }, function (err) {
      // if any of the saves produced an error, err would equal that error
      if (err) {
        console.log(err);
      } else {
        return res.send("ok");
      }
    });
});

它总是显示没有文件上传。

编辑:我的html文件代码

<form action="/file_upload" method="POST" enctype="multipart/form-data">
      <input type="file" name="photos" id="photos" accept='image/*' multiple>
      <input type="submit" value="Upload">
</form>

0 个答案:

没有答案