【问题标题】:How to download to phone storage?如何下载到手机存储?
【发布时间】:2020-02-03 11:29:19
【问题描述】:

我正在使用 rn-fetch-blob 获取 api 并将文件直接下载到我的存储中,已经创建了一个路径,我现在可以成功下载它但是当我尝试打开文件时,它显示:'无法打开文件'

下面是我的下载功能:

    download =async (item) => {
    console.log(item)
    const granted = await PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE
    )
    if (granted === PermissionsAndroid.RESULTS.GRANTED){
      let dirs = RNFetchBlob.fs.dirs;
      RNFetchBlob.config({
        fileCache: true,

        addAndroidDownloads : {
            useDownloadManager: true,
            notification : true,
            path: dirs.DownloadDir + "/Baaz_"+Math.random().toString().slice(2, 6) ,
            mime : 'application/mp4',
            mediaScannable : true,
        }
    })
.fetch('GET',  'some url'+item.nameVid,{ 'Cache-Control': 'no-store' }).then((res) => {

  alert('The file has saved!')
    console.log('The file saved to ', res.path())

})

    }
 }

我已经在我的下载图标的平面列表中调用了这个函数,如下所示:

<TouchableOpacity
              onPress= {() => this.download(item)}
                style={{
                  alignItems: 'center',
                  borderRadius: 60,
                  padding: 10,
                }}>
                <Icon name="download" size={30} color="white" />
              </TouchableOpacity>

谁能告诉我为什么会这样? 我已经更新了依赖项并添加了权限。 如果您还需要什么,请告诉我。

【问题讨论】:

    标签: reactjs react-native react-native-android react-native-flatlist


    【解决方案1】:

    在 iOS 上它与 android 有所不同,您必须在 react-native-share 库旁边使用一些东西

    安装后,您可以像这样使用 RNFetchBlob:

    const saveFile = async(uri, headers) => {
      const file = await RNFetchBlob.config({
        fileCache: true
      }).fetch('GET', uri, headers);
      const type = 'some/file-type';
      const base64File = await file.readFile('base64');
      const localUrl = `data:${type};base64,${base64File}`;
      await Share.open({
        localUrl
      });
      const filePath = file.path();
      await RNFetchBlob.fs.unlink(filePath);
    }

    【讨论】:

    • 根据android更新了我的代码,可以下载文件,但是当我尝试打开它时,它说无法打开文件,你能告诉我,为什么会这样?
    • 这可能是android无法读取文件格式本身,它是什么类型的文件格式? @TRINACHAUDHURI 我建议为此打开另一个问题,因为它与下载本身无关。
    • stackoverflow.com/questions/60039755/…你可以查看我的新问题
    【解决方案2】:

    您可以使用 openDocumentactionViewIntent 如下:

      RNFetchBlob.config({
        fileCache: true, 
        addAndroidDownloads: {
            useDownloadManager: true,
            path: 'path',
            mediaScannable: true,
            notification: true,
          },
        path:'path'
        }).fetch('GET', uri, headers)
          .then((resp) => {
            if (Platform.OS === 'ios') {
              RNFetchBlob.ios.openDocument(resp.data);
             }
            if (Platform.OS === 'android') {
              RNFetchBlob.android.actionViewIntent(resp.path(), 'application/pdf');;
            }
       }
    

    【讨论】:

      猜你喜欢
      • 2019-06-09
      • 2020-10-10
      • 1970-01-01
      • 2021-09-12
      • 1970-01-01
      • 1970-01-01
      • 2017-05-09
      • 2012-11-11
      • 2020-08-13
      相关资源
      最近更新 更多