download method
Future<Response>
download(
- String urlPath,
- dynamic savePath, {
- ProgressCallback? onReceiveProgress,
- Map<
String, dynamic> ? queryParameters, - CancelToken? cancelToken,
- bool deleteOnError = true,
- String lengthHeader = Headers.contentLengthHeader,
- dynamic data,
- Options? options,
override
Download the file and save it in local. The default http method is 'GET', you can custom it by Options.method.
urlPath
: The file url.
savePath
: The path to save the downloading file later. it can be a String or
a callback:
- A path with String type, eg 'xs.jpg'
- A callback
String Function(HttpHeaders responseHeaders)
; for example:
await dio.download(url,(HttpHeaders responseHeaders){
...
return '...';
});
onReceiveProgress
: The callback to listen downloading progress.
please refer to ProgressCallback.
deleteOnError
Whether delete the file when error occurs. The default value is true
.
lengthHeader
: The real size of original file (not compressed).
When file is compressed:
- If this value is 'content-length', the
total
argument ofonProgress
will be -1 - If this value is not 'content-length', maybe a custom header indicates the original
file size , the
total
argument ofonProgress
will be this header value.
you can also disable the compression by specifying the 'accept-encoding' header value as '*'
to assure the value of total
argument of onProgress
is not -1. for example:
await dio.download(url, './example/flutter.svg',
options: Options(headers: {HttpHeaders.acceptEncodingHeader: '*'}), // disable gzip
onProgress: (received, total) {
if (total != -1) {
print((received / total * 100).toStringAsFixed(0) + '%');
}
});
Implementation
@override
Future<Response> download(
String urlPath,
savePath, {
ProgressCallback? onReceiveProgress,
Map<String, dynamic>? queryParameters,
CancelToken? cancelToken,
bool deleteOnError = true,
String lengthHeader = Headers.contentLengthHeader,
data,
Options? options,
}) async {
throw UnsupportedError('Unsupport download API in browser');
}