Node.js已停止上传

时间:2014-07-18 19:57:49

标签: node.js express formidable

我不知所措,最近我的快递4应用程序已停止上传。任何上传文件的尝试都会导致上传:0%'它只是挂在那里,没有任何事件发生火灾,所以我甚至无法得到错误。

router.post('/uploader', function(req, res){
  var form,realFileName;
  form = formidable.IncomingForm();
  form.uploadDir = path.join(__dirname, '../' + opts.uploadDir);
  form.maxFieldsSize = 1000 * 1024 * 1024;
  form.keepExtensions = true;
  form.type = 'multipart';

  form.on('fileBegin', function(err, file){
    file.path = form.uploadDir + "/" + file.name;
    realFileName = file.name;
  });

  form.on('file',function(name, file){
  });

  form.on('error', function(err){
    res.render('uploader',{title: 'Upload Error'});
  });

  form.on('end', function (err, file){
    res.render('uploader',{title: 'Upload Success'});
  });
//res.render('uploader',{title: 'Upload Success'});
});

形式:

<form action="/uploader" method="post" enctype="multipart/form-data">
  <input type="hidden" name="MAX_FILE_SIZE" value="104857600">
  <input type="file" name="uploadCsv">
  <input type="submit" value="Upload CSV">
</form>

1 个答案:

答案 0 :(得分:1)

设置事件后,您需要致电form.parse(req);

form.on('end', function (err, file){
  res.render('uploader', {title: 'Upload Success'});
});

// --> Missing: "Start parsing the form":
form.parse(req);

有关完整示例,请参阅Formidable's example