无法读取 反应本机 >0.59 中的文件内容



我想在我的react native应用程序中上传文档,为此我使用react-native-document-picker

但是要读取文件的base64内容需要react-native-fs,它有一个依赖问题(需要react-native v0.59)

我无法安装它,因为我有react-native v 0.63

安装时出现以下错误:

admin@Salvis-MacBook project % npm install react-native-fs --save
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! 
npm ERR! While resolving: project@0.0.1
npm ERR! Found: react-native@0.63.0
npm ERR! node_modules/react-native
npm ERR!   react-native@"0.63.0" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer react-native@"^0.59.5" from react-native-fs@2.16.6
npm ERR! node_modules/react-native-fs
npm ERR!   react-native-fs@"*" from the root project
npm ERR! 
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
npm ERR! 
npm ERR! See /Users/admin/.npm/eresolve-report.txt for a full report.
npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/admin/.npm/_logs/2021-01-25T09_41_08_842Z-debug.log

如果你遇到了同样的问题,请提出建议。

我尝试使用run -fetch-blob阅读内容,它在Android上工作正常,但我无法在ios模拟器中读取文件(我从safari下载了一些示例pdf文件)

当我试图读取文件时,它说文件没有找到。

我还尝试了以下方法:

uri.replace('file://', '')
uri.replace('file:', '')
decodeURIComponent(uri)

但在ios上仍无法读取

似乎react-native-fs有一个问题(有一个开放的问题:在这里检查)

所以我找到了另一种解决方案来读取文件的内容(base64)。请使用run -fetch-blob的readFile()函数。

在需要传递base64字符串的函数中,只需执行:

const fs = RNFetchBlob.fs;
let Base64String = await fs.readFile(yourFileObject.uri, 'base64');

yourFileObject是您通过react-native-document-picker

选择任何文件时获得的对象。你可以使用你选择的任何选择器,只需要在readFile()函数中传递文件uri(本地路径),你就可以开始了。

最新更新