【发布时间】:2020-03-22 15:22:07
【问题描述】:
如何将 VueJs 中的图像和文本一起发送到我的后端 ExpressJs?
现在,我所做的是创建两个 http post 请求
注意 this.albumName 和 this.albumDesc 只是文本,formData 是图像。
createAlbum() {
const formData = new FormData();
for (let file of Array.from(this.myAlbumImages)) {
formData.append("files", file);
}
if (this.albumName) {
axios
.post("http://localhost:9001/image/album", {
ALBUM: this.albumName,
DESCRIPTION: this.albumDesc
})
.then(resp => console.log(resp))
.catch(err => console.log(err));
setTimeout(function() {
axios
.post("http://localhost:9001/image/album", formData)
.then(resp => console.log(resp))
.catch(err => console.log(err));
}, 3000);
this.albumName = "";
this.albumDesc = "";
} else {
alert("Please fill the above form.");
}
},
这是我的后端。
这会根据传递的数据创建文件夹,并且还会创建一个名为 undefined 的文件夹
router.post('/album', (req, res) => {
let sql = "INSERT INTO GALLERY SET ALBUM = ?, DESCRIPTION = ?";
let body = [req.body.ALBUM, req.body.DESCRIPTION]
myDB.query(sql, body, (error, results) => {
if (error) {
console.log(error);
} else {
let directory = `C:/Users/user/Desktop/project/adminbackend/public/${req.body.ALBUM}`;
fse.mkdirp(directory, err => {
if (err) {
console.log(err);
} else {
console.log(directory);
}
})
}
})
我认为这是因为 NodeJS 是异步的,这就是它创建未定义文件夹的原因。
【问题讨论】:
标签: javascript express http vue.js vuejs2