3

我正在尝试使用强大的模块上传文件。但是当我选择多个文件上传时,只有第一个被上传。我是节点新手。

这是我的上传功能

exports.uploadPhotos = function(req, res) {
      console.log("upload received");
      var form = new formidable.IncomingForm();
      form.multiples = true;

      form.parse(req, function(err, fields, files) {
        res.status(200, {
          'content-type': 'text/plain'
        });
        res.end('received upload:\n\n');
        res.end(util.inspect({
          fields: fields,
          files: files
        }));
      });

      form.on('end', function(fields, files) {
        /* Temporary location of our uploaded file */
        var temp_path = this.openedFiles[0].path;
        /* The file name of the uploaded file */
        var file_name = this.openedFiles[0].name;
        /* Location where we want to copy the uploaded file */
        var new_location = 'uploads/';

        fs.copy(temp_path, new_location + file_name, function(err) {
          if (err) {
            console.error(err);
          } else {
            console.log("success!");
          }
        });
      });
    };

当我调用它时,会打印以下内容

received upload:

{ fields: { itemPhoto: '' },
  files: { upload: [ [Object], [Object] ] } }

我应该怎么做才能上传所有文件?

4

0 回答 0