React native 使用axios发送文件时出现本机获取网络错误
我使用api平台创建端点来处理图像上传。 我的api需要文件类型才能发出post请求。 这是一个使用post man的post请求示例: 我想使用react native处理使用axios发送图像的问题 我创建了一个如下的post请求:React native 使用axios发送文件时出现本机获取网络错误,react-native,file-upload,axios,api-platform.com,React Native,File Upload,Axios,Api Platform.com,我使用api平台创建端点来处理图像上传。 我的api需要文件类型才能发出post请求。 这是一个使用post man的post请求示例: 我想使用react native处理使用axios发送图像的问题 我创建了一个如下的post请求: this.setState({ avatarSource: source, }); console.log(this.state.avatarSource.uri); const data = new FormData(
this.setState({
avatarSource: source,
});
console.log(this.state.avatarSource.uri);
const data = new FormData();
data.append('file', {
uri: this.state.avatarSource.uri,
// show full image path in my device
// file:///storage/emulated/0/Pictures/image-c40b64fc-6d74-46a7-9016-191aff3740dd.jpg
});
axios
.post(`${API.URL}/media_objects`, data, {
headers: {
'Content-Type': 'multipart/form-data',
},
})
.then((resp) => console.log(resp))
.catch((err) => console.log(err.message));
}
});
我正在将手机中图像的完整路径发送到api,但我收到了“网络错误”我通过在ReactNativeFlipper.java中注释这一行修复了问题:
NetworkFlipperPlugin networkFlipperPlugin = new NetworkFlipperPlugin();
NetworkingModule.setCustomClientBuilder(
new NetworkingModule.CustomClientBuilder() {
@Override
public void apply(OkHttpClient.Builder builder) {
// builder.addNetworkInterceptor(new FlipperOkhttpInterceptor(networkFlipperPlugin)); // add comment here and build android
}
});
client.addPlugin(networkFlipperPlugin);
client.start();