首页 > 解决方案 > iOS 上的 Rn-fetch-blob 使用 RNFetchBlob 上传图像

问题描述

我尝试使用以下代码在服务器上上传图像:

    updateAvatar(token, photo) {
    return postImage(
      "http://lk.skilla.ru/myapi/updateAvatar",
      [
        {
          name: "avatar",
          filename: "avatar.jpg",
          type: "image/jpg",
          data: Platform.OS === "android" ? RNFetchBlob.wrap(photo) : photo,
        },
      ],
      token
    );
  },
const postImage = (url, params, token) => {
  return RNFetchBlob.config({ fileCache: true, appendExt: "jpg" })
    .fetch(
      "POST",
      url,
      { Authorization: token, "Content-Type": "multipart/form-data" },
      params
    )
    .then((res) => {
      console.log("response");
      console.log(res.text());
        return Platform.OS === 'android' ? 'file://' + res.path() : '' + res.path()
    })
    .catch((error) => console.log(error));
};

在 android 上一切正常,但在 RN-Fetch-Blob 返回的 iOS 文件上不存在。如何在iOS上包装网址?RNFetchBlob.wrap 不起作用

标签: react-nativereact-native-iosrn-fetch-blob

解决方案


这就是我让它为我工作的方式。

const filePath = Platform.OS === 'ios'
    ? res.path().replace('file:///', '').replace('file://', '')
    : res.path().replace('file://', '').replace('file:/', '');

推荐阅读