首页 > 解决方案 > HostFunction 中的异常:使用 fetch 在 React Native 上上传图像时来自 JS 的错误调用

问题描述

我正在尝试使用 fetch 将图像上传到服务器,它在 Android 上运行良好,但在 iOS 上出现错误,上传确实有效,但始终显示此错误。

export const uploadImage = async (image: Response, organisation: string, tokenData: any) => {
  if (image.data) {
    const JWT = pathOr('', ['getFileAccessToken', 'AccessToken'], tokenData);
    // image.filename doesn't exist on android
    const filename = Platform.OS === 'ios' ? image.filename : image.path.split('/').pop();
    const buffer = Buffer.from(image.data, 'base64');
    const response = await fetch(`${config.API_ENDPOINT}/files/${filename}`, {
      method: 'POST',
      headers: {
        Authorization: `Bearer ${JWT}`,
        'Content-Type': image.mime,
        'CP-Org-Name': organisation,
      },
      body: buffer,
    });
    return response.json();
  }
};

标签: javascriptiosreact-nativefetch

解决方案


推荐阅读