0๐
<input class="form-control block" type="file" ref="files" multiple required @change="handleFileUpload">
data() {
return {
email: '',
username: '',
files: [],
isSubmitting: false,
isSubmitted: false,
isError: false,
};
},
methods: {
handleFileUpload() {
// eslint-disable-next-line
this.files = this.$refs.files.files;
},
async addWork() {
try {
this.isSubmitting = true;
this.isError = false;
const formData = new FormData();
for (let i = 0; i < this.files.length; i++) {
const file = this.files[i];
formData.append(`files['${i}']`, file);
}
const { username, email } = this;
formData.append('email', email);
formData.append('username', username);
await WorksRepository.addWork(formData);
this.isSubmitted = true;
} catch (e) {
this.isError = true;
} finally {
this.isSubmitting = false;
}
},
you can see full example here
- [Vuejs]-How to write a single piece of code running both on route A (mounted hook) and also when arriving at route A?
- [Vuejs]-Use ES6 arrow function as AJAX callback handler
Source:stackexchange.com