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

时间:2017-08-08 14:52:23

标签: react-native

我需要解析一个本地.csv文件,它包含在所有js文件之间(就像某种本地ressource)。 我找到了一个完成这项工作的图书馆> 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] } ]

好像我的资源文件夹总是空的。

1 个答案:

答案 0 :(得分:3)

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

放在里面:

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

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

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

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

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