【问题标题】:About upload image in Node.js关于在 Node.js 中上传图片
【发布时间】:2014-09-04 03:03:52
【问题描述】:

我看到一些关于这个问题的 stackoverflow。

在玉中。

form.editUserForm(name="editUser", action="/upload", method="post")
    input(placeholder="Select you photo" type="file", name="thumbnail")
    input.ui.blue.submit.button(type="submit", value="Submit")

在js中。

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

我发现“req.files”是“未定义”,所以我不能使用下面的代码。

app.post('/upload', function (req, res) {
    var tmp_path = req.files.thumbnail.path;
    target_path = '/tmp/' + req.files.thumbnail.name;
    fs.rename(tmp_path, target_path, function(err) {
        if (err)
            throw err;
        fs.unlink(tmp_path, function() {
            if (err)
                throw err;
        });
    });
});

【问题讨论】:

  • 您在该路由上方使用什么中间件?
  • 我尝试使用'强大',我解决了。

标签: javascript node.js image upload pug


【解决方案1】:

您必须将表单的enctype 属性设置为值multipart/form-data

form.editUserForm(name="editUser", action="/upload", method="post", enctype="multipart/form-data")

【讨论】:

  • 一些解决方案说'express 4.0 not support the req.files'
猜你喜欢
  • 1970-01-01
  • 2022-01-15
  • 2015-01-12
  • 1970-01-01
  • 1970-01-01
  • 2018-03-07
  • 1970-01-01
  • 1970-01-01
  • 2018-01-14
相关资源
最近更新 更多