繁体   English   中英

如何在反应本机博览会中将文件上传到服务器

[英]How to upload file to server in react native expo

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

这是我的代码

我的配置文件.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);
        }
      }
    }
  };

用户操作.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);
    });
};

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

React Native - Axios - 尝试上传图片

如何在 axios 中使用 react 设置 multipart?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM