React native 在android上的expo on react native上获取请求失败
我使用fetch将图像发送到服务器。 在iOS上,一切正常,但在android上,我的请求失败。React native 在android上的expo on react native上获取请求失败,react-native,expo,React Native,Expo,我使用fetch将图像发送到服务器。 在iOS上,一切正常,但在android上,我的请求失败。 我从expo image picker获得的uri,在IOS上一切正常 const form = new FormData(); form.append('file', { uri: imageUri, name: 'file.jpg', fileName: 'file', }); fetch(api,{ meth
我从expo image picker获得的uri,在IOS上一切正常
const form = new FormData();
form.append('file', {
uri: imageUri,
name: 'file.jpg',
fileName: 'file',
});
fetch(api,{
method: 'post',
body: form,
})
.then(response => response.text())
.then(textResponse => {
console.log(textResponse);
Alert.alert(textResponse);
if (textResponse){
setAccuracyResponse(textResponse);
}
})
.catch(error => {
console.error(error)
});
在android上,我得到了这样的回应
Network request failed
- node_modules/whatwg-fetch/dist/fetch.umd.js:535:17 in setTimeout$argument_0
- node_modules/react-native/Libraries/Core/Timers/JSTimers.js:130:14 in _callTimer
- node_modules/react-native/Libraries/Core/Timers/JSTimers.js:383:16 in callTimers
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:416:4 in __callFunction
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:109:6 in __guard$argument_0
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:364:10 in __guard
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:108:4 in callFunctionReturnFlushedQueue
* [native code]:null in callFunctionReturnFlushedQueue
世博会sdk v39世博会版本3.28.5
节点v14.15.0
npm 6.14.8这方面存在一些问题。首先,android中的文件选择器会将imageUri值作为
file:/user/.../path/to/file.jpg
file:///user/.../path/to/file.jpg.
而iOS中的文件选择器会将imageUri值作为
file:/user/.../path/to/file.jpg
file:///user/.../path/to/file.jpg.
因此,正确的方法是在android的formData中使用文件://而不是文件:
第二个问题是在表单数据中提供适当的mime类型,例如.jpg文件的mime类型为image/jpeg,而.png文件的mime类型为image/png,因此可以使用“mime”npm包来实现这一点。
因此,我的最终解决方案如下所示:
const imageUri = result.uri;
const newImageUri = Platform.OS === "android" ? "file:///" + imageUri.split("file:/").join("") : imageUri;
const form = new FormData();
form.append('file', { //this prop can vary depends of your sever settings
uri: newImageUri,
type: mime.getType(newImageUri),
name: newImageUri.split("/").pop(),
});
这方面存在一些问题。首先,android中的文件选择器会将imageUri值作为
file:/user/.../path/to/file.jpg
file:///user/.../path/to/file.jpg.
而iOS中的文件选择器会将imageUri值作为
file:/user/.../path/to/file.jpg
file:///user/.../path/to/file.jpg.
因此,正确的方法是在android的formData中使用文件://而不是文件:
第二个问题是在表单数据中提供适当的mime类型,例如.jpg文件的mime类型为image/jpeg,而.png文件的mime类型为image/png,因此可以使用“mime”npm包来实现这一点。
因此,我的最终解决方案如下所示:
const imageUri = result.uri;
const newImageUri = Platform.OS === "android" ? "file:///" + imageUri.split("file:/").join("") : imageUri;
const form = new FormData();
form.append('file', { //this prop can vary depends of your sever settings
uri: newImageUri,
type: mime.getType(newImageUri),
name: newImageUri.split("/").pop(),
});
嘿,就连我也面临着类似的问题。你有什么进展吗?@SubhangV我已经更新了我的答案,检查下面。嘿,就连我也面临着类似的问题。你有什么进展吗?@SubhangV我已经更新了我的答案,请检查下面。