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
Add a ment  | 

1 Answer 1

Reset to default 4

You 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