Skip to content

Instantly share code, notes, and snippets.

@eleazarbr
Created January 8, 2021 23:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eleazarbr/351e73d49fcda879278a5783df53bc5f to your computer and use it in GitHub Desktop.
Save eleazarbr/351e73d49fcda879278a5783df53bc5f to your computer and use it in GitHub Desktop.
How to create a FormData from an object and send an image to the backend using axios
// In this example you can see how to create a FormData from an object.
// This, in conjunction with axios, can be used to send an image to the back-end.
var formData = new FormData()
formData.append('_method', 'PUT')
for (const [key, value] of Object.entries(myObject)) {
formData.append(key, JSON.stringify(value))
}
if (this.image != null) formData.append('image', this.image)
axios.post(route('resource.update', { id: this.resource.id }), formData, {
headers: {
'Content-Type': 'multipart/form-data'
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment