Skip to content

Instantly share code, notes, and snippets.

@mbfakourii
Created October 20, 2022 11:34
Show Gist options
  • Save mbfakourii/bff505d8449d8eaa875b91cb78eee85b to your computer and use it in GitHub Desktop.
Save mbfakourii/bff505d8449d8eaa875b91cb78eee85b to your computer and use it in GitHub Desktop.
Download File (Uint8List) with blob in flutter web
// 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);
}
}
<!-- add this app.js in web/index.html -->
<head>
<script src="app.js" defer></script>
.
.
</head>
// 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