-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
mbfakourii
wants to merge
20
commits into
cfug:main
Choose a base branch
from
mbfakourii:fix_DioForBrowser
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
c429be1
feat: add support web download
mbfakourii 7147d04
fix: code format
mbfakourii 6e5a96b
fix: add `ResponseType.bytes`
mbfakourii 4dec223
fix: change implementation base `XMLHttpRequest`
mbfakourii 3853fc9
Merge branch 'main' into fix_DioForBrowser
mbfakourii 7cf567f
Merge branch 'main' into fix_DioForBrowser
mbfakourii 0b1fbe6
fix: change the implementation based on the latest changes
mbfakourii 77c63b8
fix: change the implementation based on the latest changes
mbfakourii 7bc44a1
Merge branch 'main' into fix_DioForBrowser
mbfakourii cf1c52d
Merge branch 'main' into fix_DioForBrowser
mbfakourii 1bbc7f9
fix: improve description
mbfakourii 9db2305
fix: remove listenCancelForAsyncTask
mbfakourii 5917273
Merge remote-tracking branch 'refs/remotes/origin/main' into fix_DioF…
mbfakourii d029294
fix: blob with web
mbfakourii 9f1f36e
test: add test
mbfakourii 4507ef2
Merge branch 'main' into fix_DioForBrowser
mbfakourii a71f76f
feat: add example for download blob and remove some extra code
mbfakourii 5fda084
test: fix ResponseType typo
mbfakourii 13369e1
Merge branch 'main' into fix_DioForBrowser
mbfakourii d77d692
Merge branch 'main' into fix_DioForBrowser
mbfakourii File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import 'dart:js_interop'; | ||
import 'package:web/web.dart'; | ||
|
||
void downloadBlob(String blobUrl, String name) { | ||
final Document htmlDocument = document; | ||
final HTMLAnchorElement anchor = | ||
htmlDocument.createElement('a') as HTMLAnchorElement; | ||
anchor.href = blobUrl; | ||
anchor.style.display = name; | ||
anchor.download = name; | ||
document.body!.add(anchor); | ||
anchor.click(); | ||
anchor.remove(); | ||
} |
103 changes: 103 additions & 0 deletions
103
example_flutter_app/lib/download_web/main_web_download.dart
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 |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import 'package:dio/dio.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
import 'download_blob.dart'; | ||
|
||
late Dio dio; | ||
|
||
void main() { | ||
dio = Dio( | ||
BaseOptions( | ||
connectTimeout: const Duration(hours: 3), | ||
), | ||
); | ||
|
||
dio.interceptors.add(LogInterceptor()); | ||
runApp(MyApp()); | ||
} | ||
|
||
class MyApp extends StatelessWidget { | ||
// This widget is the root of your application. | ||
@override | ||
Widget build(BuildContext context) { | ||
return MaterialApp( | ||
title: 'Flutter web download blob Demo', | ||
theme: ThemeData( | ||
primarySwatch: Colors.blue, | ||
), | ||
home: MyHomePage(title: 'Flutter web download blob Demo'), | ||
); | ||
} | ||
} | ||
|
||
class MyHomePage extends StatefulWidget { | ||
MyHomePage({ | ||
super.key, | ||
this.title = '', | ||
}); | ||
|
||
final String title; | ||
|
||
@override | ||
State<MyHomePage> createState() => _MyHomePageState(); | ||
} | ||
|
||
class _MyHomePageState extends State<MyHomePage> { | ||
var url = | ||
'https://jsoncompare.org/LearningContainer/SampleFiles/Video/MP4/sample-mp4-file.mp4'; | ||
|
||
CancelToken cancelToken = CancelToken(); | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar( | ||
title: Text(widget.title), | ||
), | ||
body: Center( | ||
child: Column( | ||
spacing: 20, | ||
children: [ | ||
ElevatedButton( | ||
child: const Text('Request'), | ||
onPressed: () async { | ||
if (cancelToken.isCancelled) { | ||
cancelToken = CancelToken(); | ||
} | ||
|
||
try { | ||
// Fetch blob | ||
final Response res = await dio.download( | ||
url, | ||
'', | ||
onReceiveProgress: (count, total) { | ||
print((count / total) * 100); | ||
}, | ||
cancelToken: cancelToken, | ||
); | ||
|
||
// Download blob | ||
downloadBlob(res.data, url.split('/').last); | ||
print('fin'); | ||
} catch (e) { | ||
print('error'); | ||
} | ||
}, | ||
), | ||
ElevatedButton( | ||
child: const Text('Cancel'), | ||
onPressed: () async { | ||
cancelToken.cancel(); | ||
}, | ||
), | ||
], | ||
), | ||
), | ||
); | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What will the result be after downloaded? Could you demonstrate how it works?
EDIT: Please also add an example somewhere in our examples so everyone can run and see how it works.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added an example in
example_flutter_app
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To test on the local web, make sure CORS is disabled.
and use local
dio
anddio_web_adapter
in example_flutter_app pubspecThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why CORS is required?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To download a file from an online server to a local server, the browser gives a CORS error. This has nothing to do with Dio. I said this for testing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm, how about a local server to scope with the minimal functionality instead of an outer source?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean to put a file locally? If we do this, the download display will be very fast and the download percentage will not be clearly visible.
Are you sure about this? This is just an example to demonstrate a feature 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A demo would not require displaying the progress, correct? The file could be as small as possible too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there are some examples for pure Dart
dio/dio_test/lib/src/test/download_tests.dart
Line 94 in 0b0f527
Just need to run it on the web.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AlexV525