反应原生fs库不写文件

时间:2018-03-28 07:48:14

标签: react-native react-native-fs

当我使用react-native-fs创建文件时,回调成功到达,但文件不在我的android / data / com.myapp / files / test.txt库中。

logcat没有错误,所以我想知道为什么我的代码与简单的App.js不起作用。

    /**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
  Platform,
  StyleSheet,
  Text,
  View,
  TouchableOpacity,
  ToastAndroid
} from 'react-native';

const instructions = Platform.select({
  ios: 'Press Cmd+R to reload,\n' +
    'Cmd+D or shake for dev menu',
  android: 'Double tap R on your keyboard to reload,\n' +
    'Shake or press menu button for dev menu',
});
var RNFS = require('react-native-fs');
 // create a path you want to write to
 var path = RNFS.DocumentDirectoryPath + '/test.txt';
export default class App extends Component {
  onSave = () => {
    RNFS.writeFile(path, 'Lorem ipsum dolor sit amet', 'utf8')
.then((success) => {
 console.log('FILE WRITTEN!');
})
.catch((err) => {
 console.log(err.message);
});
  }
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          Welcome to React Native!
        </Text>
        <TouchableOpacity onPress={() => this.onSave()}>
        <Text style={styles.instructions}>
          To get started, edit App.js
        </Text>
        </TouchableOpacity>
        <Text style={styles.instructions}>
          {instructions}
        </Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

我需要将log.txt写入app文件夹,但无法创建新文件。 我很感激每一个帮助或建议。

  

react-native init myapp   cd myapp   纱线添加反应原生fs   react-native link react-native-fs   react-native run-android

2 个答案:

答案 0 :(得分:0)

您可以尝试使用 ExternalStorageDirectoryPath 替换 DocumentDirectoryPath

答案 1 :(得分:0)

您可以将 try 与 RNFS.LibraryDirectoryPath

一起使用 <块引用>

var url = 路径 let fileName = path.split("/").pop() // console.log('文件名', 文件名) var ext = this.extention(url); 分机=“。” + 分机 [0]; const localFile = ${RNFS.LibraryDirectoryPath}/${fileName}; // console.log('localFile', localFile)

        const options = {
            fromUrl: path,
            toFile: localFile,
            fileCache: true
        };

        if (await RNFS.exists(localFile)) {
            FileViewer.open(localFile, { displayName: this.props.route.params.conversationSubject });
        } else {
            await RNFS.downloadFile(options).promise
                .then((res) => {
                    // console.log('res', res)
                    FileViewer.open(localFile);
                })
                .then((res) => {
                    // success
                    // console.log("success", res);
                })
                .catch(error => {
                    // error
                    console.log("Attachment open error: ", error);
                });
        }
相关问题