如何使用expo-react原生文件系统暂停或恢复下载



我正在使用expo文件系统下载视频。

//下载进度

const [progress, setProgress] = useState();
const [percentage, setPercentage] = useState(0);
const [isDownloading, setIsDownloading] = useState(false);
const [downloadStatus, setDownloadStatus] = useState("initial");
// This is the download callback function for detecting the download process
const callback = downloadProgress => {
const progressBytes =
downloadProgress.totalBytesWritten /
downloadProgress.totalBytesExpectedToWrite;
setProgress(progressBytes);
setPercentage((progressBytes * 100).toFixed());
console.log(progressBytes);
if (progressBytes >= 1 || progressBytes < 0) {
setProgress(0);
}
};
// The download functionality.
const downloadFile = async (endpoint, name) => {
var ext = endpoint.substr(endpoint.lastIndexOf('.') + 1);
const downloadResumable = FileSystem.createDownloadResumable(
endpoint,
FileSystem.documentDirectory + name + "." + ext,
{},
callback
);
setIsDownloading(true);

switch (downloadStatus) {
case "start":
try {
const { uri } = await downloadResumable.downloadAsync();
saveFile(uri);
setIsDownloading(false);
} catch (e) {
console.error(e);
}
break;
case "pause":
try {
await downloadResumable.pauseAsync();
} catch (e) {
console.error(e);
}
break;
case "resume":
try {
const { uri } = await downloadResumable.resumeAsync();
saveFile(uri);
setIsDownloading(false);
} catch (e) {
console.error(e);
}
break;
default:
break;
}
};

//这是一个将视频保存到多媒体的功能

constsaveFile=async(uri(=>{

const { status } = await ImagePicker.requestMediaLibraryPermissionsAsync();
if (status == "granted") {
const asset = await MediaLibrary.createAssetAsync(uri);
MediaLibrary.createAlbumAsync("Cinema.af", asset, false)
.then(() => console.log('File Saved Successfully'))
.catch(() => console.log('Error in saving file'));
}
}

下载过程和保存到库工作良好。但我无法添加暂停或恢复功能。它返回无下载对象错误。请帮帮我。

您可以创建一个下载的可重复

let fullPath = FileSystem.documentDirectory + fileLocationName
downloadResumable = new FileSystem.DownloadResumable(
linkDownload,
fullPath,
downloadSnapshot.options,
callback, //->this is the callback function 
downloadSnapshot.resumeData
);

回调函数是这样的:

const callback = (downloadProgress: { totalBytesWritten: number; totalBytesExpectedToWrite: number; }) => {
const progress = downloadProgress.totalBytesWritten / downloadProgress.totalBytesExpectedToWrite;
if (!saving && progress * 100 > current + 5) {
saving = true
current = progress * 100
saveAsync(downloadResumable).then(res => {
resumeAsync(downloadResumable).then(res1 => {
saving = false
})
})
}
try {
updateProgress(progress)
} catch (error) {
}

};

每隔5%,我就用异步存储保存下载状态

const saveAsync = async (downloadResumable: any) => {
await downloadResumable.pauseAsync();
console.log('Paused download operation, saving for future retrieval');
AsyncStorage.setItem(pathAsync, JSON.stringify(downloadResumable.savable()));
}

然后我可以用这个功能恢复它:


const resumeAsync = async (downloadResumable: any) => {
downloadResumable.resumeAsync();
}

如果你不明白什么,我可以发布我的整个代码,开箱即用,我在ios方面有一些问题,但我想我能解决。

最新更新