【问题标题】:Uploading image whilst retaining other form data上传图片同时保留其他表单数据
【发布时间】:2016-09-25 20:58:42
【问题描述】:
<form id="test-form" action="/upload" method="POST">
    <input type="file" id="wizard-picture" name="picture">
    <input name="first_name" type="text" class="form-control"  placeholder="First Name">
</form>

app.post("/upload", function(req, res){
    console.log(req.body);
});


我确实看到我的“名字”出现在 req.body 中,但没有出现在“图片”中。 我将 enctype 更改为“multipart/form-data”,一切都消失了。我得到一个空的结果。我尝试了一些像 Multer 这样的软件包,我遵循了每一步,但它仍然无法正常工作。

我想上传图片并接收来自表单的其余数据。

【问题讨论】:

    标签: node.js


    【解决方案1】:

    对于文件上传,我建议使用 Multer。为此,请参阅https://github.com/expressjs/multer。它以最佳方式处理文件上传。仅通过 req.body 获取图像是行不通的。 Multer 的示例在他们的 GitHub 上提供。

    【讨论】:

    • Multer 是一个中间件。但我想在 app.post("xx", function(req, res){ here}) 中上传。有没有什么包可以做到这一点?
    • 是的,穆特。然后调用将是 app.post ("xx", multer.smth (...), function (req, res) { stuff })。 req-variable 然后包含所有文件数据,并且文件存储在服务器上。
    猜你喜欢
    • 2015-09-17
    • 1970-01-01
    • 2018-07-23
    • 1970-01-01
    • 1970-01-01
    • 2012-12-16
    • 2018-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多