我创建了一条路由,将文件存储到数据库中,该路由是使用dropzone.js发送的。考虑到我正在即时发送多个文件,在后端部分,我正在获取数组,所以我创建了for循环,但是问题是我不想用那个for循环包装整个过程,因为它不是最佳解决方案,所以如果有人可以帮助我删除它,我将不胜感激。
这是我的空地:
Dropzone.autoDiscover = false;
$(document).ready(function() {
$('#uploadDropzone').dropzone({
url: "upload-img",
method: "post",
maxFilesize: 50,
maxFiles: 12,
parallelUploads: 12,
uploadMultiple: true,
autoProcessQueue: false,
init: function () {
let myDropzone = this;
$('#submitAllProcess').on('click', function (e) {
myDropzone.processQueue();
});
}
});
这是我的路线:
router.post('/upload-img',upload_storage_img.any(), async function(req,res,next) {
let file;
let insertFile = req.files;
for (let i = 0; i < insertFile.length; i++) {
file = insertFile[i];
let path = '/upload_img/' + file.filename;
let date = moment(new Date()).format("YYYY-MM-DD HH:mm");
let folder = path.replace('/upload_img', '');
try {
const connection = await getConnPromise(req);
let Users = new users(connection);
await Users.insert_img_to_db(file.originalname, file.filename, date, path, file.mimetype, file.filename, function (err, result) {
if (err) throw err;
console.log("success");
res.send("success");
})
}catch (e) {
console.log('blabla');
console.log(e);
}
}
});
预先感谢