【问题标题】:I want to send Array of dictionary objects containing images though formData in Fetch/Axios [closed]我想通过 Fetch/Axios 中的 formData 发送包含图像的字典对象数组 [关闭]
【发布时间】:2020-11-18 09:53:28
【问题描述】:

嘿,我想用 fetch 发送这个数据,谁能指导我怎么做?

{
    "User": "irjhiosd@#Dsadm",
    "name": "Some Name Here"
    pictures : [
       { picture : @PICTURE_FILE_HERE },
       { picture : @PICTURE_FILE_HERE },
       { picture : @PICTURE_FILE_HERE }
    ]
}

【问题讨论】:

  • 这在很大程度上取决于服务器期望数据的结构方式
  • @Quentin 完全正确。如果是多部分的,请参阅stackoverflow.com/questions/35192841/…
  • 服务器期望文件以表单数据形式发送,这是 Django 标准 API
  • @p32094 问题通常是你可以用键和值附加表单数据,我的用例值是一个包含 json 对象的数组,其中包含我不确定如何处理的文件

标签: javascript json reactjs axios fetch


【解决方案1】:
const toBase64 = file => new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => resolve(reader.result);
    reader.onerror = error => reject(error);
});

let imageArray = []
// SAY U HAVE PICTURES IN A 'pictures' ARRAY
pictures.forEach((picture)=>{
    imageArray.push(  {'picture' : picture})
})
fields['pictures'] = imageArray

// After this u can normally send feilds as body in your fetch / axios request

【讨论】:

    猜你喜欢
    • 2017-01-28
    • 2021-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-01
    • 2023-02-21
    • 2021-12-08
    • 2021-05-22
    相关资源
    最近更新 更多