【问题标题】:react native fs library not writing files反应原生 fs 库不写文件
【发布时间】:2018-09-06 19:18:48
【问题描述】:

当我使用 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 纱线添加 react-native-fs react-native 链接 react-native-fs react-native run-android

【问题讨论】:

    标签: react-native react-native-fs


    【解决方案1】:

    您可以尝试将 DocumentDirectoryPath 替换为 ExternalStorageDirectoryPath

    【讨论】:

    • 当然,我试过了,但仍然是同样的问题。 logcat 中仅显示 FILE WRITTEN 没有错误。编辑:对不起,我正在查看不同的目录。我的文件已保存到内部存储的根目录,但我认为不可能将文件保存在那里。如何上班 DocumentDirectoryPath ?
    • ExternalStorageDirectoryPath 需要额外的权限:ExternalDirectoryPath 不是更好吗?
    • 此答案不适用于 iOS
    【解决方案2】:

    你可以用RNFS.LibraryDirectoryPath试试

    var url = 路径 让 fileName = path.split("/").pop() // console.log('fileName', fileName) var ext = this.extention(url); 分机=“。” +分机[0]; 常量 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);
                    });
            }
    

    【讨论】:

      猜你喜欢
      • 2022-08-22
      • 1970-01-01
      • 2021-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-09
      相关资源
      最近更新 更多