如何从服务器将文件下载到react-native的项目代码工作区中的特定文件夹中?

时间:2019-07-18 10:14:23

标签: react-native react-native-fetch-blob

我有一个反应敏捷的项目女巫,有时我需要更新自己的资产, 我在靠近app.js的项目根目录中有一个“ app / assets”文件夹,我安装了“ react-native-fetch-blob”来下载文件,还使用它的文件系统api将其写入“ assets”文件夹中,但是我无法将其保存在文件夹中,我只能使用“ RNFetchBlob.fs.dirs.DocumentDir”,但我不知道它在哪里,也无法在我的代码中使用它, 如何将下载的文件准确地写入“资产”文件夹? 这是我的代码:

MyProp

1 个答案:

答案 0 :(得分:1)

Use this code for download image using RNFetchBlob 
more information visit 

https://www.npmjs.com/package/rn-fetch-blob?activeTab=dependents

import RNFetchBlob from 'rn-fetch-blob'

type Props = {};
export default class App extends Component<Props> {

  constructor(){
    super();
    this.state = {
        download : ''
    }
  }

  componentDidMount(){
    this._testDownload();
  }
  _testDownload = () => {

        PermissionsAndroid.request(
          PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
          {
            title: "Storage",
            message: "This app would like to store some files on your phone"
          }
        )
          .then(() => {
            let dirs =
              `/storage/emulated/0/app/assets/`


              RNFetchBlob.config({
                path: dirs + '/1.png',
                fileCache: true
              })
                .fetch("GET",' https://www.gstatic.com/webp/gallery3/1.png', {})
                .then(res => {
                  console.log("res.data============================", res.data);

                })
                .catch(err => {
                  console.log("Error ", err);
                });
            }

          })


  }

  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>Welcome to React Native!</Text>
        <Text style={styles.instructions}>To get started, edit App.js</Text>
        <Text style={styles.instructions}>{this.state.download}</Text>
      </View>
    );
  }
}