簡體   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