Expressjs bodyParser 和 connect-form

2023-12-12

我正在上传图像连接形式。但如果我使用它就不起作用bodyParser()。 相反,如果我不使用 bodyParser,我就无法上传文件?

我怎样才能让他们一起玩?这是我的配置:

app.configure(function() {
    app.register('.html', require('ejs'));
    app.set('views', __dirname + '/../views');
    app.set('view engine', 'html');
    app.use(gzippo.staticGzip(__dirname + '/../public'),{ maxAge: 86400000 });
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(form({ 
        keepExtensions: true,
        uploadDir: __dirname + '/../tmp'
    }));
    app.use(express.cookieParser());
    app.use(express.session({
        secret: 'test',
        cookie: { secure: true },
        store: new MySQLSessionStore(client.database, client.user, client.password)
    }));
    app.use(expressValidator);
    app.use(app.router);
    app.use(express.csrf());
});

如果您使用的是最新的 Express,则不需要包含 connect-form(自 Connect 1.8.x 起已弃用)。

只需在路由中使用 req.files 即可获取上传的文件,Express 会完成剩下的工作。查看这篇文章:

http://tjholowaychuk.com/post/12943975936/connect-1-8-0-multipart-support

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Expressjs bodyParser 和 connect-form 的相关文章