downloadByUrl method
下载指定 URL 到 savePath(可断点续传);未传 savePath 时落到临时文件,不做 Range 续传。
onReceiveProgress 参数为 (当前文件已下载字节, 当前文件总字节或占位)。
Implementation
Future<String> downloadByUrl(
String url, {
String? savePath,
void Function(int received, int total)? onReceiveProgress,
CancelToken? cancelToken,
}) async {
final targetPath =
savePath ??
p.join(
(await _ensureCacheDir()).path,
'${DateTime.now().millisecondsSinceEpoch}${_extractExt(url)}',
);
final ct = cancelToken ?? CancelToken();
if (savePath == null) {
await _dio.download(
url,
targetPath,
onReceiveProgress: onReceiveProgress,
cancelToken: ct,
);
return targetPath;
}
return _downloadFileWithResume(
url,
targetPath,
cancelToken: ct,
onFileProgress: (fileNow, totalF) {
onReceiveProgress?.call(
fileNow,
(totalF != null && totalF > 0) ? totalF : fileNow,
);
},
);
}