【问题标题】:Download image not working on IOS device - React Native下载图像在 IOS 设备上不起作用 - React Native
【发布时间】:2020-03-25 00:12:30
【问题描述】:

我使用 RNFetchBlobReact 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."); 
          });
    }

【问题讨论】:

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


    【解决方案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只能将文件存储到外部存储,还要注意DownloadManager只能支持GET方式,也就是说请求体会被忽略。

    【讨论】:

      【解决方案2】:

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

      您可以通过以下方式遵循从函数登录的路径: cd ~/Library/Developer/CoreSimulator/Devices...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-29
        • 1970-01-01
        • 2021-12-07
        • 1970-01-01
        相关资源
        最近更新 更多