admin管理员组文章数量:1305240
I'm trying to read a text file from my local filesystem using react-native-fs.
However, it keeps telling me the file does not exist (It does, I double checked it and tried to copy the into the run window).
'use strict';
import React, { Component } from 'react';
//import FileSystem from 'react-native-filesystem';
import RNFS from 'react-native-fs';
import {
AppRegistry,
StyleSheet,
Text,
View,
NetInfo
} from 'react-native';
export default class FactsApiFetcher extends Component
{
constructor(props)
{
super(props);
this.state =
{
fact: 'Loading...'
}
}
ponentWillMount()
{
this._getRandomFact();
}
_getRandomFact()
{
const fileContents = RNFS.read("D:\\AllRand\\facts-api-handler\\test.txt") //FileSystem.readFile("D:\\AllRand\\allfacts\\test.txt");
this.setState({
fact: fileContents
})
}
render()
{
return <Text> { this.state.fact } </Text>;
}
}
AppRegistry.registerComponent('FactsApiFetcher', () => FactsApiFetcher);
I'm trying to read a text file from my local filesystem using react-native-fs.
However, it keeps telling me the file does not exist (It does, I double checked it and tried to copy the into the run window).
'use strict';
import React, { Component } from 'react';
//import FileSystem from 'react-native-filesystem';
import RNFS from 'react-native-fs';
import {
AppRegistry,
StyleSheet,
Text,
View,
NetInfo
} from 'react-native';
export default class FactsApiFetcher extends Component
{
constructor(props)
{
super(props);
this.state =
{
fact: 'Loading...'
}
}
ponentWillMount()
{
this._getRandomFact();
}
_getRandomFact()
{
const fileContents = RNFS.read("D:\\AllRand\\facts-api-handler\\test.txt") //FileSystem.readFile("D:\\AllRand\\allfacts\\test.txt");
this.setState({
fact: fileContents
})
}
render()
{
return <Text> { this.state.fact } </Text>;
}
}
AppRegistry.registerComponent('FactsApiFetcher', () => FactsApiFetcher);
Share
Improve this question
asked Aug 5, 2017 at 7:51
omer bar levomer bar lev
4011 gold badge5 silver badges15 bronze badges
2
- Have you set up the configuration? github./itinance/react-native-fs#usage-android, and there is an example usage too there. – Hana Alaydrus Commented Aug 5, 2017 at 8:34
-
1
From the documentation github./itinance/… you need to return promise when use method
read()
– Hana Alaydrus Commented Aug 5, 2017 at 8:41
1 Answer
Reset to default 4You should bundle your files into app or put them inside Simulator or Device manually. When you run application it could explore only it's own filesystem, and it can't explre your Windows FS. Use this constants https://github./itinance/react-native-fs#constants to get access to files.
本文标签: javascriptRead local file react nativeStack Overflow
版权声明:本文标题:javascript - Read local file react native - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741751204a2395842.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论