Convert JS Object to form data

前端 未结 18 1903
孤街浪徒
孤街浪徒 2020-11-29 17:22

How can I can convert my JS Object to FormData?

The reason why I want to do this is, I have an object that I constructed out of the ~100 form field valu

18条回答
  •  囚心锁ツ
    2020-11-29 17:39

    Sorry for a late answer, but I was struggling with this as Angular 2 currently does not support file upload. So, the way to do it was sending a XMLHttpRequest with FormData. So, I created a function to do it. I'm using Typescript. To convert it to Javascript just remove data types declaration.

    /**
         * Transforms the json data into form data.
         *
         * Example:
         *
         * Input:
         * 
         * fd = new FormData();
         * dob = {
         *  name: 'phone',
         *  photos: ['myphoto.jpg', 'myotherphoto.png'],
         *  price: '615.99',
         *  color: {
         *      front: 'red',
         *      back: 'blue'
         *  },
         *  buttons: ['power', 'volup', 'voldown'],
         *  cameras: [{
         *      name: 'front',
         *      res: '5Mpx'
         *  },{
         *      name: 'back',
         *      res: '10Mpx'
         *  }]
         * };
         * Say we want to replace 'myotherphoto.png'. We'll have this 'fob'.
         * fob = {
         *  photos: [null, ]
         * };
         * Say we want to wrap the object (Rails way):
         * p = 'product';
         *
         * Output:
         *
         * 'fd' object updated. Now it will have these key-values ", ":
         *
         * product[name], phone
         * product[photos][], myphoto.jpg
         * product[photos][], 
         * product[color][front], red
         * product[color][back], blue
         * product[buttons][], power
         * product[buttons][], volup
         * product[buttons][], voldown
         * product[cameras][][name], front
         * product[cameras][][res], 5Mpx
         * product[cameras][][name], back
         * product[cameras][][res], 10Mpx
         * 
         * @param {FormData}  fd  FormData object where items will be appended to.
         * @param {Object}    dob Data object where items will be read from.
         * @param {Object =   null} fob File object where items will override dob's.
         * @param {string =   ''} p Prefix. Useful for wrapping objects and necessary for internal use (as this is a recursive method).
         */
        append(fd: FormData, dob: Object, fob: Object = null, p: string = ''){
            let apnd = this.append;
    
            function isObj(dob, fob, p){
                if(typeof dob == "object"){
                    if(!!dob && dob.constructor === Array){
                        p += '[]';
                        for(let i = 0; i < dob.length; i++){
                            let aux_fob = !!fob ? fob[i] : fob;
                            isObj(dob[i], aux_fob, p);
                        }
                    } else {
                        apnd(fd, dob, fob, p);
                    }
                } else {
                    let value = !!fob ? fob : dob;
                    fd.append(p, value);
                }
            }
    
            for(let prop in dob){
                let aux_p = p == '' ? prop : `${p}[${prop}]`;
                let aux_fob = !!fob ? fob[prop] : fob;
                isObj(dob[prop], aux_fob, aux_p);
            }
        }
    

提交回复
热议问题