You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native使用ReactNativeBlobUtil在iOS下载PDF的路径与格式问题求助

React Native ReactNativeBlobUtil iOS下载问题解决方案

问题核心原因

你的代码中fileCache: true会强制文件存入ReactNativeBlobUtil的临时缓存目录(即你看到的ReactNativeBlobUtil_tmp下的随机命名文件),而非指定的Documents目录,这导致文件路径嵌套且无后缀;同时info.plist存在配置错误,进一步影响文件访问权限。

解决方案

1. 关闭缓存,直接写入指定路径

将fileCache: true改为fileCache: false,让文件直接保存到你配置的addIOSDownloads.path路径,而非临时缓存目录。

修改后的配置片段:

const config = {
  fileCache: false, // 关键修改:关闭缓存
  addAndroidDownloads: {
    useDownloadManager: true,
    notification: true,
    path: `${dirToSave}/${fileName}`,
    description: t("downloading"),
  },
  addIOSDownloads: {
    notification: true,
    path: `${dirToSave}/${fileName}`,
    description: t("downloading"),
  },
};

2. 修正info.plist错误配置

你的配置存在两处无效问题:

  • 重复且带空格的UIFileSharingEnabled键
  • NSDownloadsFolderUsageDescription的值应为字符串而非布尔值

修正后的info.plist:

<key>UIFileSharingEnabled</key>
<true/>
<key>CFBundleGetInfoString</key>
<string></string>
<key>LSApplicationCategoryType</key>
<string></string>
<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>
<key>NSDownloadsFolderUsageDescription</key>
<string>需要访问文档目录以保存下载的文件</string>

3. 优化文件名生成逻辑

原文件名生成规则可能出现重复,改用时间戳确保唯一性:

const fileName = `download_${Date.now()}.pdf`;

4. 验证修改效果

修改后,下载完成的res.path()应返回类似如下路径:
/var/mobile/Containers/Data/Application/XXX/Documents/download_1699999999999.pdf
此时通过iOS的Files应用或iTunes文件共享即可正常看到并打开该PDF文件。

完整修改后的代码

const downloadReport = async () => {
    try {
      const { dirs } = ReactNativeBlobUtil.fs;
      const dirToSave = LAYOUT.isiOS ? dirs.DocumentDir : dirs.LegacyDownloadDir;
      const fileName = `download_${Date.now()}.pdf`;

      const config = {
        fileCache: false,
        addAndroidDownloads: {
          useDownloadManager: true,
          notification: true,
          path: `${dirToSave}/${fileName}`,
          description: t("downloading"),
        },
        addIOSDownloads: {
          notification: true,
          path: `${dirToSave}/${fileName}`,
          description: t("downloading"),
        },
      };

      const res = await ReactNativeBlobUtil.config(config)
        .fetch("GET", FILE_URL, {});
      console.log("The file saved to ", res.path());
      showSuccessMessage(t("fileDownloadedSuccessfully"));
    } catch (error) {
      showErrorMessage(t("failedToDownloadFile"));
    }
};

内容的提问来源于stack exchange,提问作者Tajinder Singh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 11:17:03