【问题标题】:Express.js File UploadsExpress.js 文件上传
【发布时间】:2013-04-30 20:23:40
【问题描述】:

我对表达很陌生,并且在使用 express.bodyParser 上传文件时遇到问题。 bodyParser 按预期与 req.body 一起工作,因此它似乎已正确设置。我正在运行节点 0.6.17 并表示 2.5.8。每当我尝试访问 req.files 时,它都是未定义的。有谁知道这个问题的原因是什么?

来自 app.js:

app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.cookieParser());
  app.use(express.session({secret: "string" }));
  app.use(flash());
  app.use( express.bodyParser() );
  app.use(expressValidator);
  app.use(express.methodOverride());
  app.use(express.static(__dirname + '/public'));
  app.use(app.router);
});

来自 index.js

app.get('/product/add', function(req, res) {
    res.render("add_products", {
      title: "Add Products",
            email: req.session.email || 'Sign In/Up',
      error: req.flash('error') || []
    });
});

app.post('/product/add', function(req, res) {
console.log(req.files) // prints undefined
var errors = generate_error_messages(req, 'product/add') || [];
if (errors.length > 0) {
  var errors_string_array = messages(errors);
  req.flash('error', errors_string_array);
  res.redirect('/product/add');
} else {
  ProductDatabase.save(req, function(err, docs) {
    res.redirect('/');
  });
}
});

add_products.jade

  form(class='form-horizontal', method='post', action='/product/add')
    fieldset
      .control-group
        label(class='control-label', for="title") Product Title
        .controls
          input(type="text", class="input-xlarge", name="title")
      .control-group
        label(class='control-label', for="description") Description
        .controls
          textarea(class="input-xlarge", name="description", rows="5")
      .control-group
        label(class='control-label', for='auction_length') Auction Length
        .controls
          select(name='auction_length')
            option 1 day
            option 2 days
            option 5 days
      .control-group
        label(class='control-label', for="fileInput") Upload Image
        .controls
          input(class='input-file', name='fileInput', type='file')
      .form-actions
        input(type="submit", class="btn btn-primary") Sell Product
        a.btn(href='/') Cancel

【问题讨论】:

  • 你考虑过使用 Formidable 吗?
  • 是的,我尝试添加 var form = new formidable.IncomingForm(); form.parse(req, function(err, fields, files) {...} 在 post 路由中,但程序从未运行过 parse 方法。
  • 强大的被express使用。

标签: javascript node.js express


【解决方案1】:

怎么样 connect-form?根据我的经验,这种方法效果更好。

【讨论】:

  • 我也试过了,但它返回 undefined 就像 bodyParser 一样
  • 在表单中,设置enctype为“multipart/form-data”
【解决方案2】:

你的 add_products.jade 文件有问题。

第一行表单标签应该有enctype属性。应该像,

form(class='form-horizo​​ntal', method='post', action='/product/add',enctype='multipart/form-data')

要发布文件,您应该具有该属性。

【讨论】:

    【解决方案3】:

    对于简单的上传,您只需要这些配置:

    app.use(express.static(__dirname + '/upload'));
    app.use(express.bodyParser({uploadDir:__dirname + '/upload'}));
    

    在玉模板中:

    form(method='post', action='/upload', enctype='multipart/form-data')
        input(name='file', type='file')
        input(type='submit')
    

    【讨论】:

      猜你喜欢
      • 2015-06-27
      • 1970-01-01
      • 2018-09-25
      • 1970-01-01
      • 1970-01-01
      • 2012-07-12
      • 2015-04-03
      • 2018-04-24
      • 2022-06-22
      相关资源
      最近更新 更多