如何使用强大的表达式一次上传多个文件

时间:2014-10-23 11:57:37

标签: javascript node.js express formidable

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

这是我的上传功能

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] ] } }

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

0 个答案:

没有答案