首页 > 解决方案 > 在调试模式下,图像和视频未使用 axios 上传到服务器

问题描述

我正在使用react-native-image-crop-picker从图库中获取图像并尝试使用 Axios 将其上传到服务器上。但是它没有上传到服务器,当我点击 api 上传它开始发送并且永无止境并且没有从服务器得到响应。但是当我尝试进行构建然后尝试上传然后成功上传并从服务器获得响应。这是我的代码。

const handleProfilePic = () => {
    const date = new Date();

    const formData = new FormData();
    formData.append('files', {
      uri: image.path,
      type: image.mime,
      name: 'image_' + Math.floor(date.getTime() + date.getSeconds() / 2),
    });
    console.log(formData);
    new Promise((rsl, rej) => {
      setLoading(true);
      updatePic(formData, user.auth, rsl, rej);
    })
      .then((res) => {
        Snackbar.show({
          text: res,
          duration: Snackbar.LENGTH_SHORT,
        });
        setLoading(false);
      })
      .catch((errorData) => {
        setLoading(false);
        Snackbar.show({
          text: errorData,
          duration: Snackbar.LENGTH_SHORT,
        });
      });
  };



//add pic code 

export const updatePic = (data, token, rsl, rej) => {
  return (dispatch) => {
    axios(`${BASE_URL}/Authentication/addpicture`, {
      method: 'post',
      data,
      headers: {
        auth: token,
      },
    })
      .then((res) => {
        console.log(res);
        if (res.data.status == true) {
          rsl(res.data.message);
        } else {
          rej(res.data.message);
        }
      })
      .catch((err) => {
        console.log(err);
        rej(err.message);
      });
  };
};

标签: javascriptreact-nativeaxiosmultipartform-datareact-native-image-picker

解决方案


我已经通过评论这一行来解决它 Open this dir 'android/app/src/debug/java/com/flatApp/ReactNativeFlipper.java'

NetworkingModule.setCustomClientBuilder(
   new NetworkingModule.CustomClientBuilder() {
     @Override
     public void apply(OkHttpClient.Builder builder) {
       // builder.addNetworkInterceptor(new FlipperOkhttpInterceptor(networkFlipperPlugin));
     }
   });

推荐阅读