Created
October 20, 2022 11:34
-
-
Save mbfakourii/bff505d8449d8eaa875b91cb78eee85b to your computer and use it in GitHub Desktop.
Download File (Uint8List) with blob in flutter web
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
// crate in web/app.js | |
// Function to download data to a file | |
function download(data, filename, type) { | |
var file = new Blob([data], {type: type}); | |
if (window.navigator.msSaveOrOpenBlob) // IE10+ | |
window.navigator.msSaveOrOpenBlob(file, filename); | |
else { // Others | |
var a = document.createElement("a"), | |
url = URL.createObjectURL(file); | |
a.href = url; | |
a.download = filename; | |
document.body.appendChild(a); | |
a.click(); | |
setTimeout(function() { | |
document.body.removeChild(a); | |
window.URL.revokeObjectURL(url); | |
}, 0); | |
} | |
} |
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 app.js in web/index.html --> | |
<head> | |
<script src="app.js" defer></script> | |
. | |
. | |
</head> |
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
// ignore_for_file: avoid_web_libraries_in_flutter | |
import 'dart:js' as js; | |
import 'dart:typed_data'; | |
void downloadWeb({required Uint8List byte,required String filename,required String mime}) { | |
js.context.callMethod('download', [byte, filename, mime]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment