Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for download function in web #2230

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
7 changes: 7 additions & 0 deletions dio/lib/src/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ enum ResponseType {

/// Get the original bytes, the [Response.data] will be [List<int>].
bytes,

/// Get blob url.
/// This value is needed when you need content in the form of a blob url.
/// Only work in web.
///
/// the [Response.data] will be [String].
blobUrl,
}

/// {@template dio.options.ListFormat}
Expand Down
44 changes: 31 additions & 13 deletions plugins/web_adapter/lib/src/html/adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ class BrowserHttpClientAdapter implements HttpClientAdapter {
xhrs.add(xhr);
xhr
..open(options.method, '${options.uri}')
..responseType = 'arraybuffer';
..responseType =
options.responseType == ResponseType.blobUrl ? 'blob' : 'arraybuffer';

final withCredentialsOption = options.extra['withCredentials'];
if (withCredentialsOption != null) {
Expand All @@ -65,18 +66,35 @@ class BrowserHttpClientAdapter implements HttpClientAdapter {
final completer = Completer<ResponseBody>();

xhr.onLoad.first.then((_) {
final Uint8List body = (xhr.response as ByteBuffer).asUint8List();
completer.complete(
ResponseBody.fromBytes(
body,
xhr.status!,
headers: xhr.responseHeaders.map((k, v) => MapEntry(k, v.split(','))),
statusMessage: xhr.statusText,
isRedirect: xhr.status == 302 ||
xhr.status == 301 ||
options.uri.toString() != xhr.responseUrl,
),
);
if (options.responseType == ResponseType.blobUrl) {
completer.complete(
ResponseBody.fromString(
Url.createObjectUrl(xhr.response),
Comment on lines +69 to +72
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you reuse other fields by constructing the ResponseBody with the unnamed constructor?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ResponseBody with the unnamed constructor requires steam, we have a url string !

xhr.status!,
headers:
xhr.responseHeaders.map((k, v) => MapEntry(k, v.split(','))),
statusMessage: xhr.statusText,
isRedirect: xhr.status == 302 ||
xhr.status == 301 ||
options.uri.toString() != xhr.responseUrl,
),
);
} else {
final Uint8List body = (xhr.response as ByteBuffer).asUint8List();
completer.complete(
ResponseBody.fromBytes(
body,
xhr.status!,
headers: xhr.responseHeaders.map(
(k, v) => MapEntry(k, v.split(',')),
),
statusMessage: xhr.statusText,
isRedirect: xhr.status == 302 ||
xhr.status == 301 ||
options.uri.toString() != xhr.responseUrl,
),
);
}
});

Timer? connectTimeoutTimer;
Expand Down
23 changes: 20 additions & 3 deletions plugins/web_adapter/lib/src/html/dio_impl.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// export '../js_interop/dio_impl.dart';

import 'dart:async';

import 'package:dio/dio.dart';

import 'adapter.dart';
Expand Down Expand Up @@ -27,9 +29,24 @@ class DioForBrowser with DioMixin implements Dio {
String lengthHeader = Headers.contentLengthHeader,
Object? data,
Options? options,
}) {
throw UnsupportedError(
'The download method is not available in the Web environment.',
}) async {
final Response response = await fetch(
RequestOptions(
baseUrl: urlPath,
data: data,
method: 'GET',
responseType: ResponseType.blobUrl,
queryParameters: queryParameters,
cancelToken: cancelToken,
onReceiveProgress: onReceiveProgress,
),
);

final completer = Completer<Response>();

// Set response in Completer
completer.complete(response);

return completer.future;
}
}