如何下载到手机存储空间?

问题描述 投票:0回答:1

我使用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个回答
0
投票

在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);
}
© www.soinside.com 2019 - 2024. All rights reserved.