gpt4 book ai didi

javascript - 读取本地文件 react native

转载 作者:行者123 更新时间:2023-11-28 14:48:20 30 4
gpt4 key购买 nike

我正在尝试使用以下命令从本地文件系统读取文本文件 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);

最佳答案

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

关于javascript - 读取本地文件 react native ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45519604/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com