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
Changes from 3 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
38 changes: 29 additions & 9 deletions dio/lib/src/dio/dio_for_browser.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import 'dart:async';
import 'dart:html';

import '../../dio.dart';
mbfakourii marked this conversation as resolved.
Show resolved Hide resolved
import '../adapters/browser_adapter.dart';
import '../cancel_token.dart';
import '../dio.dart';
import '../dio_mixin.dart';
import '../headers.dart';
import '../options.dart';
import '../response.dart';

/// Create the [Dio] instance for Web platforms.
Dio createDio([BaseOptions? options]) => DioForBrowser(options);
Expand All @@ -29,9 +27,31 @@ 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 {
options ??= DioMixin.checkOptions('GET', options);

options = options.copyWith(responseType: ResponseType.bytes);
mbfakourii marked this conversation as resolved.
Show resolved Hide resolved

// Set receiveTimeout to 48 hours because `Duration.zero` not work!
mbfakourii marked this conversation as resolved.
Show resolved Hide resolved
options = options.copyWith(receiveTimeout: const Duration(hours: 48));
mbfakourii marked this conversation as resolved.
Show resolved Hide resolved

final Response response = await request(
urlPath,
data: data,
options: options,
queryParameters: queryParameters,
cancelToken: cancelToken,
onReceiveProgress: onReceiveProgress,
);

final completer = Completer<Response>();

// Create blob url from byte data
response.data = Url.createObjectUrlFromBlob(Blob([response.data]));

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

return DioMixin.listenCancelForAsyncTask(cancelToken, completer.future);
mbfakourii marked this conversation as resolved.
Show resolved Hide resolved
}
}