Created
January 8, 2021 23:00
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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