简体   繁体   English

如何在 React Native 中上传多个文件?

[英]How to upload multiple files in React Native?

I get a problem to upload multiple file in React Native, today I try to look for many examples, but fail all, just title and content send to database, but files are fail, this is my script :我在 React Native 中上传多个文件时遇到问题,今天我尝试查找很多示例,但全部失败,仅将标题和内容发送到数据库,但文件失败,这是我的脚本:

This is data in 'this.state.files', I get this data from file picker, you can find here https://www.npmjs.com/package/react-native-file-picker这是'this.state.files'中的数据,我从文件选择器中获取这些数据,您可以在这里找到https://www.npmjs.com/package/react-native-file-picker

在此处输入图片说明

This is my post function :这是我的帖子功能: 在此处输入图片说明

This is my Service :这是我的服务: 在此处输入图片说明

Please anyone help me to solve this problem.请任何人帮我解决这个问题。

Thanks.谢谢。

Setup A Form:设置表格:

let data = new FormData();

Fill Form Out:填写表格:

this.state.selectedImages.forEach((item, i) => {
  data.append("doc[]", {
    uri: item.uri,
    type: "image/jpeg",
    name: item.filename || `filename${i}.jpg`,
  });
});

Submit Data:提交数据:

fetch(`${config.apiBase}/load/${this.props.id}/uploadconfirmation`, {
  method: "post",
  headers: {
    Accept: "application/x-www-form-urlencoded",
    Authorization: `Bearer ${this.props.user.token}`,
  },
  body: data,
}).then(res => res.json())
  .then(res => {
    Alert.alert(
      "Success",
      "Bill of Loading Uploaded Successfully!",
      [{ text: "OK", onPress: () => that.props.close() }],
      { cancelable: false }
    );
  })
  .catch(err => {
    console.error("error uploading images: ", err);
  });

This tutorial is from Ariel Salem :本教程来自 Ariel Salem:

Blockquote 块引用

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

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