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 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 29 additions & 13 deletions dio/lib/src/adapters/browser_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class BrowserHttpClientAdapter implements HttpClientAdapter {
xhrs.add(xhr);
xhr
..open(options.method, '${options.uri}')
..responseType = 'arraybuffer';
..responseType = options.blobUrl ? 'blob' : 'arraybuffer';

final withCredentialsOption = options.extra['withCredentials'];
if (withCredentialsOption != null) {
Expand All @@ -67,18 +67,34 @@ 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.blobUrl) {
completer.complete(
ResponseBody.fromString(
Url.createObjectUrl(xhr.response),
mbfakourii marked this conversation as resolved.
Show resolved Hide resolved
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 dio/lib/src/dio/dio_for_browser.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import '../adapters/browser_adapter.dart';
import '../cancel_token.dart';
import '../dio.dart';
Expand Down Expand Up @@ -29,9 +31,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',
queryParameters: queryParameters,
cancelToken: cancelToken,
onReceiveProgress: onReceiveProgress,
blobUrl: true,
),
);

final completer = Completer<Response>();

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

return DioMixin.listenCancelForAsyncTask(cancelToken, completer.future);
mbfakourii marked this conversation as resolved.
Show resolved Hide resolved
}
}
8 changes: 8 additions & 0 deletions dio/lib/src/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ class RequestOptions extends _RequestConfig with OptionsMixin {
this.onReceiveProgress,
this.onSendProgress,
this.cancelToken,
this.blobUrl = false,
super.method,
super.sendTimeout,
super.receiveTimeout,
Expand Down Expand Up @@ -526,6 +527,7 @@ class RequestOptions extends _RequestConfig with OptionsMixin {
ProgressCallback? onReceiveProgress,
ProgressCallback? onSendProgress,
CancelToken? cancelToken,
bool? blobUrl,
Map<String, dynamic>? extra,
Map<String, dynamic>? headers,
bool? preserveHeaderCase,
Expand Down Expand Up @@ -558,6 +560,7 @@ class RequestOptions extends _RequestConfig with OptionsMixin {
connectTimeout: connectTimeout ?? this.connectTimeout,
data: data ?? this.data,
path: path ?? this.path,
blobUrl: blobUrl ?? false,
baseUrl: baseUrl ?? this.baseUrl,
queryParameters: queryParameters ?? Map.from(this.queryParameters),
onReceiveProgress: onReceiveProgress ?? this.onReceiveProgress,
Expand Down Expand Up @@ -630,6 +633,11 @@ class RequestOptions extends _RequestConfig with OptionsMixin {

/// {@macro dio.options.ProgressCallback}
ProgressCallback? onSendProgress;

/// Get blob url
///
/// Only work in web
bool blobUrl;
mbfakourii marked this conversation as resolved.
Show resolved Hide resolved
}

bool _defaultValidateStatus(int? status) {
Expand Down