【问题标题】:Read local file react native读取本地文件反应原生
【发布时间】:2017-08-05 07:51:32
【问题描述】:

我正在尝试使用从本地文件系统读取文本文件 react-native-fs.

但是,它一直告诉我该文件不存在(确实如此,我仔细检查了它并尝试将其复制到运行窗口中)。

'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...'
    } 
  }   

  componentWillMount()
  {
    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);

【问题讨论】:

标签: javascript java reactjs react-native


【解决方案1】:

您应该将文件捆绑到应用程序中或手动将它们放入模拟器或设备中。当您运行应用程序时,它只能探索它自己的文件系统,而无法探索您的 Windows FS。使用这个常量https://github.com/itinance/react-native-fs#constants 来访问文件。

【讨论】:

    猜你喜欢
    • 2022-08-22
    • 2019-10-17
    • 2020-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-01
    • 2019-09-29
    • 2017-11-08
    相关资源
    最近更新 更多