downloadUri method
Future<Response>
downloadUri(
- Uri uri,
- dynamic savePath, {
- ProgressCallback? onReceiveProgress,
- 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.
uri
: 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(Headers)
; for example:
await dio.downloadUri(uri,(Headers headers){
// Extra info: redirect counts
print(headers.value('redirects'));
// Extra info: real uri
print(headers.value('uri'));
...
return "...";
});
onReceiveProgress
: The callback to listen downloading progress.
please refer to ProgressCallback.
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.downloadUri(uri, "./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> downloadUri(
Uri uri,
savePath, {
ProgressCallback? onReceiveProgress,
CancelToken? cancelToken,
bool deleteOnError = true,
lengthHeader = Headers.contentLengthHeader,
data,
Options? options,
}) {
return download(
uri.toString(),
savePath,
onReceiveProgress: onReceiveProgress,
lengthHeader: lengthHeader,
deleteOnError: deleteOnError,
cancelToken: cancelToken,
data: data,
options: options,
);
}