简体   繁体   English

Express.js文件上传

[英]Express.js File Uploads

I'm fairly new to express and am having problems with using express.bodyParser to to upload files. 我是新来表达的人,在使用express.bodyParser上传文件时遇到问题。 bodyParser works as expected with req.body, so it appears to be properly set up. bodyParser可与req.body一起正常工作,因此似乎已正确设置。 I am running node 0.6.17 and express 2.5.8. 我正在运行节点0.6.17并表示2.5.8。 Whenever I try to access req.files, it is undefined. 每当我尝试访问req.files时,它都是未定义的。 Does anyone know what the cause of this issue is? 有谁知道这个问题的原因是什么?

from app.js: 来自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);
});

from index.js 来自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 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

How about connect-form ? 连接形式怎么样? That has worked better in my experience. 根据我的经验,这种方法效果更好。

u have a problem in add_products.jade file. 您在add_products.jade文件中有问题。

First line form tag should have enctype attribute.It should be like, 第一行表单标签应具有enctype属性。

form(class='form-horizontal', method='post', action='/product/add',enctype='multipart/form-data') 形式(类='水平形式',方法='张贴',动作='/产品/添加',enctype ='多部分/形式数据')

to post a file u should have that attribute. 发布文件,您应该具有该属性。

for simple uploading you just need these configuration: 对于简单的上传,您只需要以下配置:

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

and in jade template: 在玉模板中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM