简体   繁体   English

将object数组转换为formdata

[英]Convert object array to formdata

I want to convert Object Array to Formdata in Angular.我想将 Object 数组转换为 Angular 中的 Formdata。 My object array can contain any key value pair and image as well.我的 object 数组也可以包含任何键值对和图像。

I, then have to send this formdata via POST API to backend(Express).然后,我必须通过 POST API 将此表单数据发送到后端(Express)。

[{uid: "", image: File, description: "store", price: "800"}
 {uid: "f9b37f48-cff0-44f1-aa9f-fb9766bde90b", description: "wooden sandals", price: "100"}
 {uid: "dd5adebf-06c6-4d6c-b005-2fcb0a2ca161", description: "blanket", image: File}]

I've tried alot of options but anable to achieve this.我尝试了很多选择,但能够实现这一点。

How this conversion can be done?如何进行这种转换?

Or, if I can directly send this Object Array to POST call from Angular?或者,如果我可以直接将这个 Object 数组发送到来自 Angular 的 POST 调用?

EDIT 1: image: File is a File object:编辑 1: image: File是文件 object: 在此处输入图像描述

And upload image:并上传图片:

onUploadImage(imageInput: HTMLInputElement, i, id: string) {
    if(imageInput.files && imageInput.files[0]) {
      this.image = imageInput.files[0]
      if(this.collections[i]['image'] === "" || typeof this.collections[i]['image'] === 'string') this.collections[i]['image'] = this.image
      let result = this.collectionsToAdd.some(item => item['uid'] === id)
      if(result) {
        const index = this.collectionsToAdd.indexOf(this.collectionsToAdd.find(item => item.uid === id))
        this.collectionsToAdd[index]['image'] = this.image
      }
      else {
        const obj = {
          uid: id,
          image: null
        }
        obj.uid = id
        obj.image = this.image
        this.collectionsToAdd.push(obj)
      }

      this.reader.readAsDataURL(imageInput.files[0])
      this.reader.onload = (event) => {
        this.collections[i]['imagePath'] = event.target.result
      }
    }
  }

Thanks in advance!提前致谢!

You can try this one.你可以试试这个。 Here data is your array.这里的数据是你的数组。

const formData = new FormData();

for(let i = 0; i < data.length; i += 1) {
    Object.keys(data[i]).forEach((key: string) => {
        if(key === 'image') {
            formData.append(key, JSON.stringify(data[i][key]))
         } else {
             formData.append(key, data[i][key])
         }
    })
}

I think the easiest way you can do this is with multiple HTTP request, here's the simple code我认为最简单的方法是使用多个 HTTP 请求,这是简单的代码

// your array object
let preBody = [{uid: "", image: File, description: "store", price: "800"},
  {uid: "f9b37f48-cff0-44f1-aa9f-fb9766bde90b", description: "wooden sandals", price: "100"},
  {uid: "dd5adebf-06c6-4d6c-b005-2fcb0a2ca161", description: "blanket", image: File}];

// take a array of form data
let formDataArray: FormData[] = [];

// append each object key - value in formDataArray
preBody.forEach((object, index)=>{
  formDataArray.push(new FormData());
  Object.keys(object).forEach((key)=>{
    formDataArray[index].append(key, object[key]);
  });
});

// send request for each formData
formDataArray.forEach((formData)=>{
  this.http.post('http://localhost/test/text.php', formData).subscribe();
});

Note: Your array object should not contain any object or array.注意:您的阵列 object 不应包含任何 object 或阵列。

I hope, it may help我希望,它可能会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM