Skip to content

Commit

Permalink
1. Support post Stream<List<int>>
Browse files Browse the repository at this point in the history
2. Add some assert
  • Loading branch information
duwen committed Feb 22, 2019
1 parent 2bbbd0a commit bfae835
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion example/download.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ main() async {
"https://cdn.jsdelivr.net/gh/flutterchina/[email protected]/docs/imgs/book.jpg";

// var url = "https://www.baidu.com/img/bdlogo.gif";
await download1(dio, url, "./example/book1.jpg");
// await download1(dio, url, "./example/book1.jpg");
await download1(dio, url, (HttpHeaders headers)=>"./example/book1.jpg");
//await download2(dio, url, "./example/book2.jpg");
}
Expand Down
17 changes: 14 additions & 3 deletions package_src/lib/src/dio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,8 @@ class Dio {
response.headers = response.data.headers;
File file;
if (savePath is Function) {
assert(savePath is String Function(HttpHeaders),
"savePath callback type must be `String Function(HttpHeaders)`");
file = File(savePath(response.headers));
} else {
file = File(savePath.toString());
Expand Down Expand Up @@ -783,9 +785,18 @@ class Dio {
if (data != null && ["POST", "PUT", "PATCH"].contains(options.method)) {
// Handle the FormData
int length;
if(data is Stream<List<int>>){
stream=data;
}else if (data is FormData) {
if (data is Stream) {
assert(
data is Stream<List<int>>, "Stream type must be `Stream<List<int>>`");
stream = data;
options.headers.keys.any((String key) {
if (key.toLowerCase() == HttpHeaders.contentLengthHeader) {
length = int.parse(options.headers[stream].toString());
return true;
}
return false;
});
} else if (data is FormData) {
options.headers[HttpHeaders.contentTypeHeader] =
'multipart/form-data; boundary=${data.boundary.substring(2)}';
stream = data.stream;
Expand Down

0 comments on commit bfae835

Please sign in to comment.