1

我使用RNFetchBlob在React Native中实现了下载图像的功能,它在 android 上运行良好,但在 IOS 设备上它不工作。

以下是我用于下载功能的本机代码。

downloadImg = (url) => {
      var that = this;
      async function requestCameraPermission() {
        const granted = await PermissionsAndroid.request(
          PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
          {
            title: 'Test App',
            message: 'Test App needs access to your gallery ',
          }
        );
        if (granted === PermissionsAndroid.RESULTS.GRANTED) {
          that.actualDownload(url);
        } else {
          Alert.alert('Permission Denied!', 'You need to give storage permission to download the file');
        }
      }
      if (Platform.OS === 'android') { 
        requestCameraPermission();
      } else {
        this.actualDownload(url);
      }
}


actualDownload = (url) => {
          var date = new Date();
          var image_URL = url;
          var ext = this.getExtention(image_URL);
          ext = "." + ext[0];
          const { config, fs } = RNFetchBlob;
          let PictureDir = Platform.OS === 'ios' ? fs.dirs.DocumentDir : fs.dirs.PictureDir;
          let options = {
          fileCache: true,
          addAndroidDownloads: {
              useDownloadManager: true,
              notification: true,  
              path: PictureDir + "/Images/image_" + Math.floor(date.getTime()
              + date.getSeconds() / 2) + ext,
              description: 'Image'
          }
          }
          config(options).fetch('GET', image_URL).then((res) => {
              console.log("Thank you","Image Downloaded Successfully."); 
          });
    }
4

2 回答 2

1

addAndroidDownloads 仅适用于 android。当您使用 addAndroidDownloads 时,配置中的路径并非无用。但是对于ios,必须添加路径。试试下面的代码,你可以为ios添加进度。

actualDownload = (url) => {
          var date = new Date();
          var image_URL = url;
          var ext = this.getExtention(image_URL);
          ext = "." + ext[0];
          const { config, fs } = RNFetchBlob;
          let PictureDir = Platform.OS === 'ios' ? fs.dirs.DocumentDir : fs.dirs.PictureDir;
          let options = {
          fileCache: true,
          path: PictureDir + "/Images/image_" + Math.floor(date.getTime() // add it
          addAndroidDownloads: {
              useDownloadManager: true,
              notification: true,  
              path: PictureDir + "/Images/image_" + Math.floor(date.getTime()
              + date.getSeconds() / 2) + ext,
              description: 'Image'
          }
          }
          config(options).fetch('GET', image_URL).then((res) => {
              console.log("Thank you","Image Downloaded Successfully."); 
          });
    }

这里的文件说:

使用 DownloadManager 时,config 中的 fileCache 和 path 属性不会生效,因为 Android DownloadManager 只能将文件存储到外部存储,还要注意 Download Manager 只能支持 GET 方法,这意味着请求体将被忽略。

于 2019-11-29T11:58:21.707 回答
0

该方法也适用于 ios。通知没有来。如果您想检查它是否已下载,您可以通过在终端(Mac)或 cmd(Windows)中记录路径并通过终端本身访问它来检查它。它可能在 finder (Mac) 或文件资源管理器 (Windows) 中不可见。

您可以按照这种方式从函数中记录的路径:cd ~/Library/Developer/CoreSimulator/Devices...

于 2020-10-08T13:24:09.987 回答