【问题标题】:Save Blob to FileSystem in React Native Expo在 React Native Expo 中将 Blob 保存到文件系统
【发布时间】:2020-02-28 02:06:58
【问题描述】:

我正在尝试下载 PDF 并通过“expo-sharing”SDK 共享它。我无法使用FileSystem.createDownloadResumable,因为该文档可通过 POST 请求获得。

【问题讨论】:

    标签: javascript react-native expo


    【解决方案1】:

    将其下载为blob 并使用FileReader 将其转换为base64 字符串以传递给FileSystem.writeAsStringAsync

    const response = await axios.post(URL_PDF_CONTENT, { Benefits: payload }, { responseType: 'blob' });
    
    const fr = new FileReader();
    fr.onload = async () => {
      const fileUri = `${FileSystem.documentDirectory}/pdf.pdf`;
      await FileSystem.writeAsStringAsync(fileUri, fr.result.split(',')[1], { encoding: FileSystem.EncodingType.Base64 });
      Sharing.shareAsync(fileUri);
    };
    fr.readAsDataURL(response.data);
    

    【讨论】:

    • 经过大约 6 小时的反复试验,可以确认这是目前唯一适用于通过 POST 方法获取 PDF 的 Expo 管理项目的解决方案。 Fetch 不行,FileSystem asyncDownload 只支持 GET 请求(不支持 POST),rn-fetch-blob 不兼容 Expo。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    • 1970-01-01
    • 2021-06-26
    • 1970-01-01
    相关资源
    最近更新 更多