How use formData.append for a array in typescript

后端 未结 4 471
别那么骄傲
别那么骄傲 2021-01-18 06:09

Hi i want to send a form to my endpoint Profile, my problem is in the field user:{}, because i can\'t find the way to put my array into this field.

this are the fiel

相关标签:
4条回答
  • 2021-01-18 06:58

    FormData's append() method can only accept objects of string or blob type. If you need to append the array, use JSON.stringify() method to convert your array into a valid JSON string.

    Here is how:

    formData.append('user', JSON.stringify(body['user']));
    

    Here you can read more about JavaScript's JSON object.

    0 讨论(0)
  • 2021-01-18 07:02

    I had similar problem and I had declare array in key. For your example it would looks like this:

    formData.append('user[id]', body['user']['id']);
    ...
    

    It can be usefull if you wanten't or you can't parse json on server site

    0 讨论(0)
  • 2021-01-18 07:04

    I had to add four images to same formdata id. Below is my code

    const files = event.target.files;
      for(var i=0;i< files.length;i++){
        this.formData.append("imgs",files[i]);
      }
    
    0 讨论(0)
  • 2021-01-18 07:16

    try like this.. it works in my project

     var formData= new FormData();
    
        for (let file of files) {
            formData.append('photo[]', file);
        }
    
    0 讨论(0)
提交回复
热议问题