1

私は表現するのがかなり初めてで、express.bodyParser を使用してファイルをアップロードする際に問題があります。bodyParser は req.body で期待どおりに動作するため、適切に設定されているように見えます。ノード 0.6.17 と Express 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
4

3 に答える 3

0

接続フォームはどう ですか?それは私の経験ではうまくいきました。

于 2012-05-18T14:22:18.393 に答える
0

簡単なアップロードの場合は、次の構成が必要です。

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')
于 2013-04-30T12:23:40.047 に答える
0

add_products.jade ファイルに問題があります。

最初の行のフォーム タグには enctype 属性が必要です。

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

ファイルを投稿するには、その属性が必要です。

于 2013-03-09T05:30:49.233 に答える