繁体   English   中英

在 React Native 中创建和下载文本文件

[英]Create and download text file in react native

我想在客户端(应用程序)的同一侧将字符串转换为文本文件并将其下载到我的手机。 我怎样才能做到这一点?

你可以使用react-native-fs来完成这个任务。 它为 react-native 提供本机文件系统访问。

var RNFS = require('react-native-fs');

var path = RNFS.DocumentDirectoryPath + '/test.txt';

// write the file
RNFS.writeFile(path, 'your text comes here', 'utf8')
  .then((success) => {
    console.log('FILE WRITTEN!');
  })
  .catch((err) => {
    console.log(err.message);
  });

这可能会帮助您:

var fs = require('react-native-fs');

  const readFile = async () => {
    // console.log('fs.DocumentDirectoryPath', fs.DocumentDirectoryPath);
    fs.mkdir(fs.DocumentDirectoryPath)
      .then(() => downloadWithRNFS())
      .catch(err => console.log(err));
  };

  const downloadWithRNFS = async () => {
    const options = {
      fromUrl: 'https://facebook.github.io/react-native/img/header_logo.png',
      toFile: `${fs.DocumentDirectoryPath}/test.txt`,
    };
    fs.downloadFile(options)
      .promise.then(res => {
        console.log('downloadWithRNFS', res);
      })
      .catch(err => console.log(err));
  };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM