【问题标题】:Uploading files with ExpressJS使用 ExpressJS 上传文件
【发布时间】:2012-09-18 19:10:30
【问题描述】:

我正在使用最新的 NodeJS 和 ExpressJS 编写一个小应用程序。目前我坚持上传文件。 :>

路由是这样配置的

app.get('/Share', share.index);
app.post('/Share/Process', share.processFile);

索引视图如下所示

form.form-horizontal(action='/Share/Process', method='post')
  legend share a file
  div.control-group
    label.control-label(for='item[actual]') File
    div.controls
      input(type='file', name='item[actual]', required='required')

当我遵循 ExpressJS API 文档时,我应该可以使用 req.files 访问该文件,这在 share.processFile 方法中是未定义的

exports.processFile = function(req,res){
  console.log(req.files); // undefined
  console.log(req.body.item.actual); // filename as string
  res.render('share/success', {navigation: { shareClass : 'active'}, downloadKey: 'foo'});
};

【问题讨论】:

  • 您确定要在定义路线之前调用express.bodyParser(否则不会设置req.files)?
  • 是的,bodyParser 是在没有任何参数的情况下调用的

标签: javascript node.js express


【解决方案1】:

尝试将表单编码更改为multipart/form-data,IIRC 应该设置为传输文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-02
    • 1970-01-01
    • 2020-07-08
    • 2012-10-06
    • 2016-10-12
    • 1970-01-01
    • 2014-07-06
    相关资源
    最近更新 更多