代码之家  ›  专栏  ›  技术社区  ›  Wai Yan Hein

resact Native RNFetchBlob在下载后获取文件的URI

  •  0
  • Wai Yan Hein  · 技术社区  · 5 年前

    我正在开发一个本地项目。我现在要做的是下载并将下载的文件保存到设备中。我在用这个包裹, https://www.npmjs.com/package/rn-fetch-blob 用于下载文件。

    这是我的密码

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

    enter image description here

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

    {
      uri: res.path()
    }
    

    0 回复  |  直到 5 年前
        1
  •  0
  •   Pankti Shah    5 年前

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

    
          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);
        });
    
    
    推荐文章