react-native - Expo 文件系统 pauseAsync 给出错误 'No download object available'

标签 react-native expo

我正在使用 expo-file-system 创建一个可恢复的下载。我使用的示例与世博会文档中的示例相同。

https://docs.expo.io/versions/latest/sdk/filesystem/

下载部分运行良好。它会下载文件并提供进度。但是当我尝试使用 pauseAsync 函数时,它给出了错误。

'没有可用的下载对象'

export default function App() {
const [downloadProgress,setDownloadProgress] = useState({})

const callback = downloadProgress => {
const progress = downloadProgress.totalBytesWritten / downloadProgress.totalBytesExpectedToWrite;
console.log(progress);
setDownloadProgress(progress)
};

const downloadResumable = FileSystem.createDownloadResumable(
'http://techslides.com/demos/sample-videos/small.mp4',
FileSystem.documentDirectory + 'small.mp4',
{},
callback
);

startDownload = async() =>{

try {
  const { uri } = await downloadResumable.downloadAsync();
  console.log('Finished downloading to ', uri);
} catch (e) {
  console.error(e);
}
}

pauseDownload = async() =>{
try {
  await downloadResumable.pauseAsync();
  console.log('Paused download operation, saving for future retrieval');
  AsyncStorage.setItem('pausedDownload', JSON.stringify(downloadResumable.savable()));
} catch (e) {
  console.error(e);
}
}


return (
<View style={{    flex: 1,backgroundColor: '#fff',alignItems: 'center',justifyContent: 'center',marginTop: 20}}>
  <TouchableNativeFeedback onPress = {this.startDownload}>
    <View style={{alignItems:'center',justifyContent:'center',height:50,width:100,backgroundColor:'#00f',marginBottom:2}}>
      <Text style={{color:'#fff'}}>Download</Text>
    </View>
  </TouchableNativeFeedback>

  <TouchableNativeFeedback onPress = {this.pauseDownload}>
    <View style={{alignItems:'center',justifyContent:'center',height:50,width:100,backgroundColor:'#00f',marginBottom:2}}>
      <Text style={{color:'#fff'}}>Pause</Text>
    </View>
  </TouchableNativeFeedback>
</View>
);
}

这是我遇到的错误。 Error image here

最佳答案

你的问题只是你没有使用 useRef 钩子(Hook)来保存你的 resumableDownload 对象。所以我认为你的反对意见被驳回了。因为您收到 NO DOWNLOAD AVAILABLE 错误。 useRef 帮助了我,而 pauseAsync 为我正确地工作了。

//creating downloadResumable.
    const resumableDownload = useRef(FileSystem.createDownloadResumable(
        'http://d23dyxeqlo5psv.cloudfront.net/big_buck_bunny.mp4',
        FileSystem.documentDirectory + 'small.mp4',
        {},
        callback,
      ));

  //pausing donwload

  const pauseDownload = async () => {
    try {
     await resumableDownload.current.pauseAsync();
       
        console.log('Paused download operation, saving for future retrieval');
        AsyncStorage.setItem('pausedDownload', JSON.stringify(resumableDownload.current.savable()));
      } catch (e) {
        console.error(e);
      }
}

关于react-native - Expo 文件系统 pauseAsync 给出错误 'No download object available',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58133948/

相关文章:

javascript - 在 react-native (expo) 上禁用原生抖动效果

listview - React Native ListView 不会在数据更改时更新

javascript - 通过蓝牙连接设备

reactjs - 检查是否在设备React Native EXPO上禁用了声音

javascript - 如何在 componentWillReceiveProps 上运行 Lottie 动画?

react-native - react native : Build apk file using Expo CLI

ios - 文字超出 iPhone 上的 View

android - ReactNative 找不到 Android SDK

android - react native : Android back button does not work when the screen re mounts

javascript - 有没有办法通过 React-Native (JS) 预打包 Realm 数据库 (default.Realm)