如何使用react-native访问JSON以外的本地文件

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

我需要解析一个本地的.csv文件,它包含在所有js文件之间(就像某种本地资源)。我找到了一个完成工作的图书馆> Papaparse。但由于与Javascript-Environment不兼容,似乎无法“下载”我的本地文件。

我想如果我能以某种方式将我本地csv文件的内容作为字符串传递给papaparse,它会起作用。但我无法弄清楚如何使用react-native在变量中获取本地csv文件的内容。有什么建议?

编辑:我用react-native-fs尝试过。我将我的csv文件放在android / src / main / assets /下并尝试使用RNFS.readDirAssets('/');访问它

- >输出是这样的:08-11 14:53:14.524 17058 17202 I ReactNativeJS: [ { ctime: null, 08-11 14:53:14.524 17058 17202 I ReactNativeJS: mtime: Invalid Date, 08-11 14:53:14.524 17058 17202 I ReactNativeJS: name: 'AndroidManifest.xml', 08-11 14:53:14.524 17058 17202 I ReactNativeJS: path: '//AndroidManifest.xml', 08-11 14:53:14.524 17058 17202 I ReactNativeJS: size: 0, 08-11 14:53:14.524 17058 17202 I ReactNativeJS: isFile: [Function: isFile], 08-11 14:53:14.524 17058 17202 I ReactNativeJS: isDirectory: [Function: isDirectory] }, 08-11 14:53:14.524 17058 17202 I ReactNativeJS: { ctime: null, 08-11 14:53:14.524 17058 17202 I ReactNativeJS: mtime: Invalid Date, 08-11 14:53:14.524 17058 17202 I ReactNativeJS: name: 'META-INF', 08-11 14:53:14.524 17058 17202 I ReactNativeJS: path: '//META-INF', 08-11 14:53:14.524 17058 17202 I ReactNativeJS: size: 0, 08-11 14:53:14.524 17058 17202 I ReactNativeJS: isFile: [Function: isFile], 08-11 14:53:14.524 17058 17202 I ReactNativeJS: isDirectory: [Function: isDirectory] }, 08-11 14:53:14.524 17058 17202 I ReactNativeJS: { ctime: null, 08-11 14:53:14.524 17058 17202 I ReactNativeJS: mtime: Invalid Date, 08-11 14:53:14.524 17058 17202 I ReactNativeJS: name: 'assets', 08-11 14:53:14.524 17058 17202 I ReactNativeJS: path: '//assets', 08-11 14:53:14.524 17058 17202 I ReactNativeJS: size: 0, 08-11 14:53:14.524 17058 17202 I ReactNativeJS: isFile: [Function: isFile], 08-11 14:53:14.524 17058 17202 I ReactNativeJS: isDirectory: [Function: isDirectory] }, 08-11 14:53:14.524 17058 17202 I ReactNativeJS: { ctime: null, 08-11 14:53:14.524 17058 17202 I ReactNativeJS: mtime: Invalid Date, 08-11 14:53:14.524 17058 17202 I ReactNativeJS: name: 'classes.dex', 08-11 14:53:14.524 17058 17202 I ReactNativeJS: path: '//classes.dex', 08-11 14:53:14.524 17058 17202 I ReactNativeJS: size: 0, 08-11 14:53:14.524 17058 17202 I ReactNativeJS: isFile: [Function: isFile], 08-11 14:53:14.524 17058 17202 I ReactNativeJS: isDirectory: [Function: isDirectory] }, 08-11 14:53:14.524 17058 17202 I ReactNativeJS: { ctime: null, 08-11 14:53:14.524 17058 17202 I ReactNativeJS: mtime: Invalid Date, 08-11 14:53:14.524 17058 17202 I ReactNativeJS: name: 'lib', 08-11 14:53:14.524 17058 17202 I ReactNativeJS: path: '//lib', 08-11 14:53:14.524 17058 17202 I ReactNativeJS: size: 0, 08-11 14:53:14.524 17058 17202 I ReactNativeJS: isFile: [Function: isFile], 08-11 14:53:14.524 17058 17202 I ReactNativeJS: isDirectory: [Function: isDirectory] }, 08-11 14:53:14.524 17058 17202 I ReactNativeJS: { ctime: null, 08-11 14:53:14.524 17058 17202 I ReactNativeJS: mtime: Invalid Date, 08-11 14:53:14.524 17058 17202 I ReactNativeJS: name: 'res', 08-11 14:53:14.524 17058 17202 I ReactNativeJS: path: '//res', 08-11 14:53:14.524 17058 17202 I ReactNativeJS: size: 0, 08-11 14:53:14.524 17058 17202 I ReactNativeJS: isFile: [Function: isFile], 08-11 14:53:14.524 17058 17202 I ReactNativeJS: isDirectory: [Function: isDirectory] }, 08-11 14:53:14.524 17058 17202 I ReactNativeJS: { ctime: null, 08-11 14:53:14.524 17058 17202 I ReactNativeJS: mtime: Invalid Date, 08-11 14:53:14.524 17058 17202 I ReactNativeJS: name: 'resources.arsc', 08-11 14:53:14.524 17058 17202 I ReactNativeJS: path: '//resources.arsc', 08-11 14:53:14.524 17058 17202 I ReactNativeJS: size: 0, 08-11 14:53:14.524 17058 17202 I ReactNativeJS: isFile: [Function: isFile], 08-11 14:53:14.524 17058 17202 I ReactNativeJS: isDirectory: [Function: isDirectory] } ]

看起来我的assets文件夹总是空的。

react-native
1个回答
3
投票

我设法访问了我的csv文件:

放在里面:

android/app/src/main/assets/myFile.csv

(我自己创建了资源文件夹)

我能够使用以下代码将csv-data作为字符串获取:

const csvFile = RNFS.readFileAssets('myFile.csv', 'ascii').then(file => {
            return file;
        });

在我的情况下,文件是ascii编码的,所以我把编码'ascii'作为第二个参数,否则我会得到一些错误说“无效的连续字节”。

© www.soinside.com 2019 - 2024. All rights reserved.