vue-upload-component
vue-upload-component copied to clipboard
How can get files and post to form in one request
I want to upload images with some input in my form in one request.
How can I do that?
Did anyone know please help!
I'm hoping to do the same, would be great to get some help
Here is how I solved this problem:
appendFormData() {
const {files} = this.$refs.photos;
const fields = JSON.stringify(omit(this.form, ['photos']));
const formData = new FormData();
for (let i = 0; i < files.length; i++) {
formData.append(`files[${i}]`, files[i].file);
}
formData.append('mainData', fields);
return formData;
},
save() {
const formData = this.appendFormData();
savePromise(
formData
).then(() => {
console.log('SUCCESS!!');
}).catch(() => {
console.log('FAILURE!!');
});
}
On the backend I'm using Koa2, so I can access the uploaded files by ctx.request.files