繁体   English   中英

类型错误:网络请求失败。 如何 Append 数组中的多个图像正确形成数据。 反应本机

[英]TypeError: Network request failed. How to Append Multiple Images in Array to formData properly. react native

我是新来的本地人。 我正在尝试将数组中的 append 多个图像转换为 formData。 像这样

 formData.append('application_copy_file', 
          [{
            uri: this.state.ApplicationCopy,
            name: 'upload_application_copy.jpg',
            type: 'image/*'
          },
          {
            uri: this.state.ApplicationCopy1,
            name: 'upload_application_copy1.jpg',
            type: 'image/*'
          },
          {
            uri: this.state.ApplicationCopy2,
            name: 'upload_application_copy2.jpg',
            type: 'image/*'
          },
        ]);

但是当我append这样的时候。 当我提交表单时出现这样的错误 = [TypeError: Network request failed]

当我只上传一个这样的=

formData.append('application_copy_file', 
           {
            uri: this.state.ApplicationCopy,
            name: 'upload_application_copy.jpg',
            type: 'image/*'
          }
          );

然后它工作正常但说 = count(): Parameter must be an array or an object that implements Countable. 来自服务器 response.text。 请帮忙。

像这样尝试

formData.append('application_copy_file[0]', 
       {
        uri: this.state.ApplicationCopy,
        name: 'upload_application_copy.jpg',
        type: 'image/*'
      }
      );
formData.append('application_copy_file[1]', 
       {
        uri: this.state.ApplicationCopy1,
        name: 'upload_application_copy.jpg',
        type: 'image/*'
      }
      );

如果我假设您 state 中的所有内容仅用于上传。

你可以做,

Object.keys(this.state).forEach((item, i) => {
      formData.append(`application_copy_file[${i}]`, 
       {
        uri: this.state[item],
        name: this.state[item] + '.jpg',
        type: 'image/*'
      }
      );
})

您将必须传递图像数组,因此将数组放入JSON.stringify(your images array)

暂无
暂无

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

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