downloadByUrl method

Future<String> downloadByUrl(
  1. String url, {
  2. String? savePath,
  3. void onReceiveProgress(
    1. int received,
    2. int total
    )?,
})

下载指定 URL 的文件到 savePath,返回本地路径

onReceiveProgress 下载进度回调,参数为 (已接收字节数, 总字节数),total 可能为 -1 表示未知

Implementation

Future<String> downloadByUrl(
  String url, {
  String? savePath,
  void Function(int received, int total)? onReceiveProgress,
}) async {
  final targetPath = savePath ??
      p.join(
        (await _ensureCacheDir()).path,
        '${DateTime.now().millisecondsSinceEpoch}${_extractExt(url)}',
      );
  await _dio.download(
    url,
    targetPath,
    onReceiveProgress: onReceiveProgress,
  );
  return targetPath;
}