我正在尝试使用express和multer上传图片。这是我用来上传图片的表单:
<form action="/uploadProfilePicture" method="POST" id="form-upload-profile-picture">
<div class="row center">
<div class="row">
<input type="file" name="uploadProfilePicture" id="add-friend-search-input">
</div>
<div class="row">
<button class="button-green-large" class="u-full-width" id="add-friend-search-submit">Upload</button>
</div>
</form>
这是我的后端:
var mkdirp = require("mkdirp"),
multer = require("multer");
// MULTER CONFIGURATION
var storage = multer.diskStorage({
destination: function (req, file, cb) {
//var code = JSON.parse(req.body.model).empCode;
var dest = "/public/images";
mkdirp(dest, function (err) {
console.log(dest);
if (err) cb(err, dest);
else cb(null, dest);
});
},
filename: function (req, file, cb) {
cb(null, Date.now()+'-'+file.originalname);
}
});
// POST ROUTE: Upload profile picture
app.post("/uploadProfilePicture", upload.any(), function(req, res) {
console.log(req.body);
res.send(req.files);
});
但是,每当我上传图片时,它都不会出现在/ public / images中。我的代码出了什么问题?非常感谢!
答案 0 :(得分:3)
enctype="multipart/form-data"
。如果没有它,它会将您的表单发布为application / x-www-form-urlencoded,它不包含任何文件数据。