问题描述
Android测试机上传图片时报错(iOS正常):
{"error": "TypeError: Network request failed", "status": "FETCH_ERROR"}
问题分析
上传图片使用库是"react-native-image-crop-picker": "^0.39.0"
问题解决
react-native-image-crop-picker返回的image.sourceURL和filename只在iOS下可用,所以代码改成这样:
const filePath = Platform.OS === 'ios' ? image.sourceURL : image.path;
const fileName =
Platform.OS === 'ios' ? image.filename : image.path.split('/').pop();
let formData = new FormData();
const file = {
uri: filePath,
type: 'multipart/form-data',
name: fileName,
};
formData.append('file', file);