Javascript 如何在React Native中上载多个文件?
我在React Native中上载多个文件时遇到问题,今天我尝试查找许多示例,但全部失败,仅标题和内容发送到数据库,但文件失败,这是我的脚本: 这是“This.state.files”中的数据,我从文件选择器中获取这些数据,您可以在这里找到 这是我的帖子功能: 这是我的服务: 请任何人帮我解决这个问题 谢谢。设置表单:Javascript 如何在React Native中上载多个文件?,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我在React Native中上载多个文件时遇到问题,今天我尝试查找许多示例,但全部失败,仅标题和内容发送到数据库,但文件失败,这是我的脚本: 这是“This.state.files”中的数据,我从文件选择器中获取这些数据,您可以在这里找到 这是我的帖子功能: 这是我的服务: 请任何人帮我解决这个问题 谢谢。设置表单: let data = new FormData(); 填写表格: this.state.selectedImages.forEach((item, i) => {
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:
设置表单:
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: