forked from cfug/dio
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. refactor progress listening for sending/receiving data
2. Add ResponseType.bytes
- Loading branch information
duwen
committed
Feb 13, 2019
1 parent
8112f31
commit df95745
Showing
15 changed files
with
321 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,25 +14,45 @@ main() async { | |
// var url = "http://download.dcloud.net.cn/HBuilder.9.0.2.macosx_64.dmg"; | ||
|
||
// This is a image, about 4KB | ||
var url = "https://flutter.io/assets/flutter-lockup-4cb0ee072ab312e59784d9fbf4fb7ad42688a7fdaea1270ccf6bbf4f34b7e03f.svg"; | ||
//var url = "https://github.com/wendux/tt"; //404 | ||
//var url = "https://flutter.io/assets/flutter-lockup-4cb0ee072ab312e59784d9fbf4fb7ad42688a7fdaea1270ccf6bbf4f34b7e03f.svg"; | ||
var url = | ||
"https://cdn.jsdelivr.net/gh/flutterchina/[email protected]/docs/imgs/book.jpg"; | ||
await download1(dio, url, "./example/book1.jpg"); | ||
await download2(dio, url, "./example/book2.jpg"); | ||
} | ||
|
||
Future download1(Dio dio, String url, String savePath) async { | ||
try { | ||
await dio.download( | ||
url, | ||
"./example/book.jpg", | ||
onReceiveProgress: showDownloadProgress, | ||
); | ||
} catch (e) { | ||
print(e); | ||
} | ||
} | ||
|
||
Future download2(Dio dio, String url, String savePath) async { | ||
try { | ||
Response response = await dio.download( | ||
Response response = await dio.get<List<int>>( | ||
url, | ||
"./example/flutter.svg", | ||
onProgress: (received, total) { | ||
if (total != -1) { | ||
print((received / total * 100).toStringAsFixed(0) + "%"); | ||
} | ||
}, | ||
cancelToken: CancelToken(), | ||
options: Options( | ||
//receiveDataWhenStatusError: false, | ||
headers: {HttpHeaders.acceptEncodingHeader: "*"}, | ||
), | ||
onReceiveProgress: showDownloadProgress, | ||
//Received data with List<int> | ||
options: Options(responseType: ResponseType.bytes), | ||
); | ||
print("download succeed!"); | ||
File file = new File(savePath); | ||
var raf = file.openSync(mode: FileMode.write); | ||
// response.data is List<int> type | ||
raf.writeFromSync(response.data); | ||
raf.close(); | ||
} catch (e) { | ||
print(e.response.data); | ||
print(e); | ||
} | ||
} | ||
|
||
void showDownloadProgress(received, total) { | ||
if (total != -1) { | ||
print((received / total * 100).toStringAsFixed(0) + "%"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,22 @@ | ||
import 'package:dio/dio.dart'; | ||
import 'dart:async'; | ||
import 'dart:io'; | ||
|
||
import 'package:dio/dio.dart'; | ||
|
||
main() async { | ||
var dio = new Dio(); | ||
dio.interceptors.add(LogInterceptor(responseBody: false)); | ||
dio.get("https://github.com/wendux/tt?aa=b",queryParameters: {"kk":"tt"}).catchError(print); | ||
dio.interceptors.add(LogInterceptor(responseBody: true)); | ||
dio.get( | ||
"http://www.dtworkroom.com/doris/1/2.0.0/test", | ||
queryParameters: {"kk": "tt"}, | ||
options: Options( | ||
headers: {HttpHeaders.acceptEncodingHeader: "*"}, | ||
responseType: ResponseType.bytes | ||
), | ||
onReceiveProgress: (received, total) { | ||
if (total != -1) { | ||
print((received / total * 100).toStringAsFixed(0) + "%"); | ||
} | ||
}, | ||
).catchError(print); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.