选择文件代码:
SelectFile= async () => {
try{
const res_file = await DocumentPicker.pick({
type: [DocumentPicker.types.allFiles],
});
this.setState({ file: res_file
file_type: res_file.type,
file_name:res_file.name,
});
}
catch (err) {
if (DocumentPicker.isCancel(err)) {
} else {
throw err;
}
}
}
使用发布方法在数据库中选择图像存储数据后:
onSubmit(){
const data = new FormData();
data.append('user_id', this.state.id);
if(this.state.file != '' )
{
data.append("file",{
name: this.state.file.name,
type: this.state.file.type,
uri: this.state.file
});
data.append("passport",{
name: this.state.passport_name,
type: this.state.passport_type,
uri: this.state.passport
});
let res = fetch( 'My Api path',{
method: 'POST',
mode: 'cors',
cache: 'no-cache',
credentials: 'same-origin',
redirect: 'follow',
referrer: 'no-referrer',
body: data,
},
).then(response => response.json());
res.then(response => {
console.log(response)
});
}
}
我的API正常运行,其他发布请求正常运行。
我尝试了很多类似的事情:
return fetch
...
但是它给出了同样的问题,为什么会导致网络失败。