Created
November 28, 2018 13:10
-
-
Save crystrk/8ac1a8f570fee107f68e2df9b31def17 to your computer and use it in GitHub Desktop.
mixin : convert object / array to formData
This file contains hidden or 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
export default { | |
methods: { | |
objectToFormData(obj, form, namespace) { | |
var fd = form || new FormData(); | |
var formKey | |
for(var property in obj) { | |
if(obj.hasOwnProperty(property)) { | |
if(namespace) { | |
formKey = namespace + '[' + property + ']'; | |
} else { | |
formKey = property; | |
} | |
// if the property is an object, but not a File, | |
// use recursivity. | |
if(typeof obj[property] === 'object' && !(obj[property] instanceof File)) { | |
this.objectToFormData(obj[property], fd, property); | |
} else { | |
// if it's a string or a File object | |
fd.append(formKey, obj[property]); | |
} | |
} | |
} | |
return fd; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment