将Axios置于不同的版本,以查看问题是否持续下去。
为后端带来了潜在的CORS问题,但问题仍然存在。
BELOW是我的图像上传函数的简化版本:
const handleUpload = async (formData) => {
try {
const token = await getFromAsyncStorage(Keys.AUTH_TOKEN);
const response = await axios.post(
"http://<my-ip-address>/auth/profile-image-upload",
formData,
{
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "multipart/form-data;",
},
}
);
return response.data;
} catch (error) {
console.error(error);
throw error;
}
};
// Request camera permissions
const hasPermission = await requestCameraPermissionsAsync();
if (!hasPermission) return;
// Open the camera with ImagePicker
const result = await ImagePicker.launchCameraAsync({
cameraType: ImagePicker.CameraType.front,
allowsEditing: true, // Allows editing the picture
aspect: [1, 1], // Square aspect ratio
quality: 0.2, // Quality
});
// If the user doesn't cancel, set the selected image
if (result.canceled) return;
setIsLoading(true);
const file = result.assets[0];
const formData = new FormData();
formData.append("avatar", {
uri: file.uri,
type: "image/jpeg",
name: "profile",
} as any);
const data = await handleUpload(formData);