Android 如何在react native expo中将文件上载到服务器

Android 如何在react native expo中将文件上载到服务器,android,react-native,Android,React Native,我正在尝试从我的react本机应用程序将文件上载到服务器。我使用ImagePicker获取文件,但无法将其上载到服务器上。我每次服务器给我400。 任何帮助都会很好 这是我的密码 MyProfile.tsx userActions.ts 您需要使用contetType:multipart/formdata 这可能对你有帮助 const handleProfileUpdate = async () => { if (Platform.OS !== "web")

我正在尝试从我的react本机应用程序将文件上载到服务器。我使用
ImagePicker
获取文件,但无法将其上载到服务器上。我每次服务器给我400。 任何帮助都会很好

这是我的密码

MyProfile.tsx

userActions.ts


您需要使用contetType:multipart/formdata
这可能对你有帮助

const handleProfileUpdate = async () => {
    if (Platform.OS !== "web") {
      const { status } = await ImagePicker.requestCameraRollPermissionsAsync();
      if (status !== "granted") {
        alert("Sorry, we need camera Permissions");
      } else {
        const result = await ImagePicker.launchImageLibraryAsync({
          mediaTypes: ImagePicker.MediaTypeOptions.Images,
          allowsEditing: true,
          aspect: [4, 3],
          quality: 1,
          //base64: true,
        });

        const localUri = result.uri;
        console.log("ImagePIcker", localUri);
        if (!result.cancelled) {
          setProfileImage(localUri);
          updateProfile(localUri);
        }
      }
    }
  };
export const updateUserProfilePicture = (url: string) => async (
  dispatch: any
) => {

  var data = new FormData();  
  data.append("profilepic", url);
  console.log("profileUrl", url);
  const _rest = new restServices();
  const token = await _rest.getAccessToken();
  
  var config: AxiosRequestConfig = {
    method: "put",
    url: "http://xxxxxxxxxx/user/profilePic",
    headers: {
      Authorization: "Bearer " + token,
    },
    data: data,
  };
  axios(config)
    
    .then((res) => {
      console.log("profileUpdate", res);
    })
    .catch((err) => {
      console.log("profileupdate", err);
    });
};