我正在尝试从2个不同的输入字段上传图像。甚至可以通过2个不同的输入发送图像吗?
以下是我尝试过的multer代码:
const storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, "./views/img/temp_folder");
},
filename: function(req, file, cb) {
cb(
null,
file.fieldname + "-" + Date.now() + path.extname(file.originalname)
);
}
});
const fileFilter = function(req, file, cb) {
if (file.mimetype === "image/jpeg" || file.mimetype === "image/png") {
cb(null, true);
} else {
cb(null, false);
}
};
const upload = multer({
storage: storage,
fileFilter: fileFilter
}).array(
{ name: "imgs_to_upload", maxCount: 12 },
{ name: "main_img", maxCount: 1 }
);
这是我的HTML / PUG代码:
div(class="col-xl-9 col-lg-9 col-md-9")
form(action="" method="POST" enctype="multipart/form-data")
h3 Dodaj naslov članka
div(class="form-group")
input(type="text", name="title" placeholder="Naslov clanka" class="form-control" required)
h3 Dodaj predopis članka
div(class="form-group")
textarea(rows="10" name = "pretext")
h3 Dodaj tekst članka
div(class="form-group")
textarea(rows="20" name = "text")
div(class="form-group")
input(type="file" name = "imgs_to_upload" multiple id = "slike")
div(class="form-group")
input(type="file" name = "main_img" multiple id = "slike")
div(class="form-group" id = "button-holder")
input(type="Submit" value = "Dodaj" class = "btn btn-primary" id = "submitButton")