I have a method for Vuejs component:
async submit () {
if (this.$refs.form.validate()) {
let formData = new FormData()
formData.append('userImage', this.avatarFile, this.avatarFile.name)
this.avatarFile = formData
try {
let response = await this.$axios.post('http://localhost:3003/api/test.php', {
avatar: this.avatarFile,
name: this.name,
gender: this.gender,
dob: this.DOB,
}, {
headers: {
'Content-Type': 'multipart/form-data; boundary=' + formData._boundary
}
})
if (response.status === 200 && response.data.status === 'success') {
console.log(this.response)
}
} catch (e) {
console.log(e)
}
}
}
And in test.php
, I'm using json_decode(file_get_contents("php://input"), TRUE);
to read data as $_POST
variables.
While I am able to read name
, gender
and dob
correctly, I can't fetch avatar
properly.
Any solutions for the same?
Note: I don't to append every variable as formData.append(.., ..)
as I am planning to handle over 14 variables.
Note for moderators: I didn't find any question where formData was being used along with other data objects.