文件移动到 IOS 不起作用 - React Native FS

问题描述 投票:0回答:1

我已经实现了将图像列表作为 pdf 下载到移动存储的方法,android 工作正常,没有任何问题,但 ios 不工作。我为两个操作系统授予了所有必要的权限,但 iOS 无法工作。

const downloadAndSavePDFs = async () => {
    setDownloadingToDevice(true);
    const externalStorageDirectory =
      Platform.OS === 'android'
        ? RNFS.DownloadDirectoryPath
        : RNFS.DocumentDirectoryPath;

    const imageArray = downloadImages.map((item: any) => item.image);

    const pdfContent = imageArray
      .map((base64Image: any) => `<img src=${base64Image} />`)
      .join('');

    

    const htmlContent = `
            <!doctype html>
            <html>
              <body>
                 <div class="header">
                  <h3>${
                    toggleCheckBox
                      ? t('10017') && t('10017') !== '10017'
                        ? t('10017') + ': ' + voucher.archiveNo
                        : 'Archive number: ' + voucher.archiveNo
                      : ''
                  }</h3>
                </div>
                <div class="pdf-content">
                  ${pdfContent}
                </div>
              </body>
            </html>
          `;

    const options = {
      html: htmlContent,
      fileName:
        voucher.clientID.toString() + ' - ' + voucher.archiveNo.toString(),
      directory: 'Documents',
    };

    const pdfFile: any = await RNHTMLtoPDF.convert(options);

    const downloadsPath = `${externalStorageDirectory}/${
      voucher.clientID + ' - ' + voucher.archiveNo
    }.pdf`;

    try {
      Platform.OS === 'android' &&
        (await RNFS.moveFile(pdfFile.filePath, downloadsPath));

      
      setDownloadingToDevice(false);
    } catch (error) {
      console.log(error);
     

      setDownloadingToDevice(false);
    }

有人可以帮我解决这个问题吗?

ios react-native file download react-native-fs
1个回答
0
投票

验证您用于移动和保存 PDF 文件的文件路径是否适合 iOS。虽然“文档”是您正在使用的位置,但在 iOS 上,它可能需要类似于“库/缓存”或其他合适的目录。请参阅 RNFS 文档,了解 iOS 特有的路径。

const options = {
html: htmlContent,
  fileName: voucher.clientID.toString() + ' - ' + voucher.archiveNo.toString(),
  directory: Platform.OS === 'android' ? 'Documents' : 'Library/Caches',
};

要根据平台以不同方式处理文件路径,请使用条件语句。

const downloadsPath = `${
  externalStorageDirectory === RNFS.DocumentDirectoryPath
    ? RNFS.LibraryDirectoryPath
    : externalStorageDirectory
}/${voucher.clientID} - ${voucher.archiveNo}.pdf`;

确保您的应用程序具有在 iOS 上执行文件操作所需的权限。检查 iOS 特定的权限,并确保您的应用程序的 Info.plist 文件中有适当的条目。

<key>NSDocumentsFolderUsageDescription</key>
<string>Your app needs access to the Documents folder for storing PDFs.</string>
© www.soinside.com 2019 - 2024. All rights reserved.