【问题标题】:Image upload with react native axios使用 react native axios 上传图片
【发布时间】:2021-01-29 21:41:53
【问题描述】:

我正在尝试通过 axios 将图像上传到 API,但我遇到了服务器根本没有发送响应没有错误这是代码的问题

图像选择器

const showImage = () => {
ImagePicker.showImagePicker(options, (response) => {
  if (response.didCancel) {
    setIsImageEdit(false);
  } else if (response.error) {
  } else if (response.customButton) {
  } else {
    const source = response;
    setUserImage({
      fileLink: source.uri,
    });
    setUploadImageFile(source.uri);
  }
});

};

以及通过axios上传的代码

setActivity(true);
const token = await getToken();
if (uploadImageFile) {
  const uploadUri =
    Platform.OS === 'ios'
      ? uploadImageFile.replace('file://', '')
      : uploadImageFile;

  const data = new FormData();
  data.append('file', {
    uri: uploadImageFile,
    name: 'file',
    type: 'image/jpg',
  });
  console.log(uploadImageFile);
  console.log(data);
  Axios.post('http://capi.beebl.io/user/image', data, {
    headers: {
      Authorization: token,
      'Content-Type': 'multipart/form-data',
    },
  })
    .then((res) => {
      console.log(res);
      setActivity(false);
    })
    .catch((err) => {
      console.log(err);
      setActivity(false);
    });

没有错误显示,活动指示灯亮,一无响应

我正在通过邮递员上传图片,上传的一切都很好

【问题讨论】:

  • 它在哪里? console.log() 响应?抓到成功了吗?
  • 它不记录响应或错误它只是休眠我已经等了 10 分钟了
  • 你的uploadImageFile uri 路径是什么?
  • file:///Users/Hammad/Library/Developer/CoreSimulator/Devices/9E062C72-236F-401C-BB89-1A57D19D5DD0/data/Containers/Data/Application/84137CB5-D59A-4A56-808A -9C15C7889406/Documents/images/C37C98C0-0C60-46AE-9DD7-22D6EBBF3DE2.jpg
  • 尝试从 uri 中删除 "file://" 然后尝试

标签: react-native api axios react-native-ios react-native-image-picker


【解决方案1】:

尝试代替 uploadImageFile.replace('file://', '') 这个:uploadImageFile.replace('file:', '')

【讨论】:

    猜你喜欢
    • 2021-03-10
    • 2020-08-17
    • 2020-10-22
    • 2019-03-26
    • 2021-01-13
    • 2018-03-26
    • 2019-10-06
    • 2020-03-13
    • 2019-04-05
    相关资源
    最近更新 更多