Angular Convert JSON To FormData Code Example


Example: Convert JS Object to form data Ask Question

function buildFormData(formData, data, parentKey) {   if (data && typeof data === 'object' && !(data instanceof Date) && !(data instanceof File)) {     Object.keys(data).forEach(key => {       buildFormData(formData, data[key], parentKey ? `${parentKey}[${key}]` : key);     });   } else {     const value = data == null ? '' : data;      formData.append(parentKey, value);   } }  function jsonToFormData(data) {   const formData = new FormData();    buildFormData(formData, data);    return formData; }  const my_data = {   num: 1,   falseBool: false,   trueBool: true,   empty: '',   und: undefined,   nullable: null,   date: new Date(),   name: 'str',   another_object: {     name: 'my_name',     value: 'whatever'   },   array: [     {       key1: {         name: 'key1'       }     }   ] };  jsonToFormData(my_data)

Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable