downloadFile method
Future<bool>
downloadFile(
- String url,
- String savePath, {
- int? receiveTimeout = downLoadReceiveTimeout,
- Options? options,
- CancelToken? cancelToken,
- ProgressCallback? onReceiveProgress,
下载文件
[url] 下载地址
[savePath] 本地保存的地址
[receiveTimeout] 下载文件超时时间ms
[options] 针对当前请求的配置选项
[onReceiveProgress] 下载进度
return 是否下载成功
Implementation
Future<bool> downloadFile(
String url,
String savePath, {
int? receiveTimeout = downLoadReceiveTimeout,
Options? options,
CancelToken? cancelToken,
ProgressCallback? onReceiveProgress,
}) async {
LogUtil.v(_tag, 'downloadFile() url:$url savePath:$savePath');
bool isSuccess;
///添加CancelToken,用于取消请求
cancelToken ??= CancelToken();
_cancelTokenList.add(cancelToken);
///设置超时时间
if (receiveTimeout != null) {
options ??= Options();
options.receiveTimeout = receiveTimeout;
}
try {
await _dio.download(url, savePath,
cancelToken: cancelToken,
onReceiveProgress: onReceiveProgress,
options: options);
isSuccess = true;
LogUtil.v(_tag, 'downloadFile() success, url:$url savePath:$savePath');
} catch (e) {
LogUtil.v(_tag, 'downloadFile fail:$e');
isSuccess = false;
}
///请求完成移除cancelToken
_cancelTokenList.remove(cancelToken);
return isSuccess;
}