在React Native中上传多个文件时遇到问题,今天我尝试查找许多示例,但是全部失败,只是标题和内容发送到数据库,但是文件失败,这是我的脚本:
这是'this.state.files'中的数据,我是从文件选择器中获取的,您可以在这里https://www.npmjs.com/package/react-native-file-picker
请任何人帮助我解决此问题。
谢谢。
答案 0 :(得分:0)
设置表格:
let data = new FormData();
填写表格:
this.state.selectedImages.forEach((item, i) => {
data.append("doc[]", {
uri: item.uri,
type: "image/jpeg",
name: item.filename || `filename${i}.jpg`,
});
});
提交数据:
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);
});
本教程来自Ariel Salem: