【问题标题】:Expressjs bodyParser and connect-formExpressjs bodyParser 和 connect-form
【发布时间】:2012-01-18 20:41:28
【问题描述】:

我正在使用连接表单上传图片。但如果我使用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());
});

【问题讨论】:

    标签: javascript node.js express


    【解决方案1】:

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

    只需在您的路由中使用 req.files 即可获取上传的文件,其余的由 Express 完成。看看这篇文章:

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

    【讨论】:

    • 哦,太好了 :) 但现在我的文件名有问题。使用req.files.image.filename,我在上传之前获得了图像的名称。它被重命名为随机字符串或之后的东西。那个套装在哪里?似乎找不到关于该主题的任何内容?
    • 您将获得图像的名称和路径。只需将该图像从临时位置移动到所需位置即可:)
    • 链接失效了,最好把答案放在这里而不是粘贴链接
    • 链接现在是tjholowaychuk.tumblr.com/post/12943975936/… 但我认为整个方法现在已经过时了,搜索node.js express multipart。我最终使用了 multer
    猜你喜欢
    • 2017-06-20
    • 1970-01-01
    • 1970-01-01
    • 2018-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-15
    • 2011-12-03
    相关资源
    最近更新 更多