【问题标题】:How to upload file to server in react native expo如何在反应原生博览会中将文件上传到服务器
【发布时间】:2021-02-16 00:40:54
【问题描述】:

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

这是我的代码

MyProfile.tsx

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);
        }
      }
    }
  };

userActions.ts

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);
    });
};

【问题讨论】:

    标签: android react-native


    【解决方案1】:

    你需要使用 contetType:multipart/form-data
    这可能对你有帮助,

    React Native - Axios - Trying to upload image

    How do I set multipart in axios with react?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 2021-05-26
      • 1970-01-01
      相关资源
      最近更新 更多