React Native RNFetchBlob 在下载后获取文件的 URI [英] React Native RNFetchBlob getting the URI of the file after download

查看:107
本文介绍了React Native RNFetchBlob 在下载后获取文件的 URI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 React Native 项目.我现在要做的是将下载的文件下载并保存到设备.我正在使用这个包,

我正在尝试将其转换为要使用 Image 组件显示的 URI.我尝试将以下状态对象绑定到 Image 组件.

<代码>{uri: res.path()}

它没有用.这就是为什么作为下一次尝试,我试图将路径转换为 ​​URI 并显示 uri.我该怎么做?

解决方案

尝试提供下载文件的路径,

<预><代码>const directoryFile = RNFS.ExternalStorageDirectoryPath + "/FolderName/";RNFS.mkdir(目录文件);const urlDownload = input.url;让文件名;fileName = "文件名.zip";让目录 = 目录文件 + 文件名;RNFetchBlob.config({//如果有访问权限,响应数据将保存到此路径.路径:目录}).fetch("GET", urlDownload, {//一些标题..}).then(res => {console.log("文件保存到", res.path());//RNFS.moveFile(dirs, directoryFile + fileName);//->如果它仍然没有存储在您想要的路径中,请取消注释此行alert("文件下载到文件夹");}).catch(错误 => {console.log("错误:" + err);});

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?

解决方案

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);
    });

这篇关于React Native RNFetchBlob 在下载后获取文件的 URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆