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

lsmepo6l  于 5个月前  发布在  iOS
关注(0)|答案(1)|浏览(63)

我已经实现了将图像列表以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);
    }

字符串
有人能帮我解决这个问题吗?

mutmk8jj

mutmk8jj1#

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

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`;


调整处理错误的方式,以记录有关传输文件时出现的任何问题的全面详细信息。这可以帮助您确定确切的问题。

try {
  Platform.OS === 'android' && (await RNFS.moveFile(pdfFile.filePath, downloadsPath));
  setDownloadingToDevice(false);
} catch (error) {
  console.error('Error moving file:', error);
  setDownloadingToDevice(false);
}


使用RNFS.MainBundlePath进行阅读:如果您正在从主包中阅读PDF文件,则可能需要使用RNFS.MainBundlePath作为源路径,而不是pdfFile. filePath。

const pdfFile: any = await RNHTMLtoPDF.convert(options);
const sourcePath = Platform.OS === 'ios' ? RNFS.MainBundlePath : pdfFile.filePath;

// ...

try {
  Platform.OS === 'android' && (await RNFS.moveFile(sourcePath, downloadsPath));
  setDownloadingToDevice(false);
} catch (error) {
  console.error('Error moving file:', error);
  setDownloadingToDevice(false);
}


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

<key>NSDocumentsFolderUsageDescription</key>
<string>Your app needs access to the Documents folder for storing PDFs.</string>

相关问题