download method
void
download({
- required String savePath,
- ProgressCallback? onReceiveProgress,
- Success? success,
- Failure? failure,
- Completed? completed,
下载文件
savePath
文件保存路径
Implementation
void download({
required String savePath,
ProgressCallback? onReceiveProgress,
Success? success,
Failure? failure,
Completed? completed,
}) async {
if (!(await _checkNetWork())) {
return;
}
String url = _path.toString();
if (isRestfulUrl()) {
url = NetUtils.restfulUrl(_path.toString(), _params);
}
try {
_options?.method = _httpType.name;
///局部头优先与全局
if (_headers.isNotEmpty) {
_options?.headers = _headers;
}
if (_enableGlobalHeader) {
_options?.headers ??= {};
_options?.headers?.addAll(_rxNet._globalHeader);
}
if(_toFormData){
_bodyData = FormData.fromMap(_params);
}
if(_toBodyData){
_bodyData = _params;
}
final response = await _rxNet.client!.download(url, savePath,
onReceiveProgress: (received, total) {
if (total != -1) {
onReceiveProgress?.call(received, total);
}
///下载完成
if(received>=total){
success?.call(savePath, SourcesType.net);
}
},
queryParameters: (isRestfulUrl() || _toFormData || _toBodyData) ? {} : _params,
data: _bodyData,
options: _options,
cancelToken: _cancelToken);
onResponse?.call(response);
///失败
if (response.statusCode != 200) {
failure?.call(response.data);
return;
}
} catch (e, s) {
_catchError<T>(success, failure, null, e, s);
}finally{
completed?.call();
}
}