【问题标题】:Why RNFS can't write file as a doc file in iOS?为什么 RNFS 不能在 iOS 中将文件写入 doc 文件?
【发布时间】:2019-08-06 10:55:32
【问题描述】:

我正在尝试打开使用 RNFS 编写的 doc 文件。但我无法打开该文件。当我尝试在 iOS 中使用应用程序一词打开文件时,出现此错误,显示“无法打开文件,文件格式与文件扩展名不匹配”。

 var path = RNFS.DocumentDirectoryPath + "/test.doc";
 RNFS.writeFile(
  path,
  "Project Details\n",
  "utf8"
 );

但这在 android 中运行良好。

【问题讨论】:

    标签: react-native react-native-ios react-native-fs


    【解决方案1】:

    iOS 仅提供 .doc 只读文件。你必须使用.docx

    import { Platform } from 'react-native';
    var path = Platform.OS === 'ios' ? RNFS.DocumentDirectoryPath + "/test.docx"  : RNFS.DocumentDirectoryPath + "/test.doc";
    

    【讨论】:

    • 我试过了,但还是不行。这将创建一个名为 test.docx 的文件,但是当我尝试打开它时出现此错误“无法打开文件。抱歉,出了点问题,无法打开 test.docx”
    • 不,创建文件后,您可以从 iPhone 的文件管理器中找到该文件。
    • 你检查过文件的大小吗?
    • 是的,它只有 468 B。
    • 你的电脑上也没有打开这个文件吗?
    【解决方案2】:

    您可以在 iOS 中使用LibraryDirectoryPath 写入文件

    const localFile = ${RNFS.LibraryDirectoryPath}/${fileName}

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-01
      • 2015-02-23
      • 1970-01-01
      • 1970-01-01
      • 2021-09-12
      • 2014-02-08
      • 1970-01-01
      相关资源
      最近更新 更多