React Native RNFetchBlob getting the URI of the file after download

夙愿已清 提交于 2020-08-07 07:59:11

问题


I am developing a React Native project. What I am trying to do now is that I am downloading and saving the downloaded file to the device. I am using this package, https://www.npmjs.com/package/rn-fetch-blob for downloading the file.

This is my code

RNFetchBlob.config({
        fileCache: true,
      })
      .fetch('GET', 'https://static.standard.co.uk/s3fs-public/thumbnails/image/2016/05/22/11/davidbeckham.jpg?w968', {

      })
      .then((res) => {
        Alert.alert(res.path());
      })

After download, res.path returns the path like this.

I am trying to convert it to the URI to be displayed using the Image component. I tried binding the following state object to the Image component.

{
  uri: res.path()
}

It did not work. That is why as a next attempt, I am trying to convert the path into URI and display the uri. How can I do that?


回答1:


Try providing the path of the file where you want to download it,


      const directoryFile = RNFS.ExternalStorageDirectoryPath + "/FolderName/";
      RNFS.mkdir(directoryFile);
      const urlDownload = input.url;
      let fileName;      
      fileName = "filename.zip";  
      let dirs = directoryFile + fileName;
      RNFetchBlob.config({
        // response data will be saved to this path if it has access right.
        path: dirs
      }).fetch("GET", urlDownload, {
      //some headers ..
    })
    .then(res => {
        console.log("The file saved to ", res.path());
        //RNFS.moveFile(dirs, directoryFile + fileName); // -> uncomment this line if it still does not store at your desired path
        alert("File Downloaded At Folder");
    })
    .catch(err => {
      console.log("Error: " + err);
    });



来源:https://stackoverflow.com/questions/56690156/react-native-rnfetchblob-getting-the-uri-of-the-file-after-download

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!