vue-upload-component icon indicating copy to clipboard operation
vue-upload-component copied to clipboard

How can get files and post to form in one request

Open bocanhcam opened this issue 6 years ago • 2 comments

I want to upload images with some input in my form in one request.

How can I do that?

Did anyone know please help!

bocanhcam avatar Feb 13 '19 07:02 bocanhcam

I'm hoping to do the same, would be great to get some help

Gray93 avatar Nov 11 '19 12:11 Gray93

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

outOFFspace avatar Feb 11 '20 14:02 outOFFspace