Level 16
Does this help How can I upload image to server using axios
Hello , I try to upload a file :
<input v-on:change="onImageChange" type="file" class="custom-file-input" :id="'customFile'+id">
(...)
updateCompany(id) {
axios.post('/api/companies/' + id, {
_method: 'patch',
company: this.companyData,
imageAvatar:this.imageAvatar
}).then((response) => {
this.$refs.closeModal.click();
this.companyUpdatedConfirmation();
})
},
onImageChange(e) {
console.log(e.target.files[0]);
this.imageAvatar = e.target.files[0];
console.log(this.imageAvatar);
}
But, in update controller the result is always empty and when I :
dd($request->hasFile('imageAvatar')); it returns false !
Any suggestion ?
If you want to sent files in an XHR request, then you must setup a FormData object:
updateCompany(id) {
let form = new FormData();
Object.keys(this.form).forEach((key) => {
form.append(key, this.form[key])
})
form.append('imageAvatar', this.imageAvatar);
axios({
method: 'patch',
url: '/api/companies/' + id,
data: form
})..then((response) => {
this.$refs.closeModal.click();
this.companyUpdatedConfirmation();
})
Please or to participate in this conversation.