Skip to content

Instantly share code, notes, and snippets.

@Hesamedin
Last active March 16, 2022 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hesamedin/d4d9c42c486b2ec8aab7e6c0771d25d1 to your computer and use it in GitHub Desktop.
Save Hesamedin/d4d9c42c486b2ec8aab7e6c0771d25d1 to your computer and use it in GitHub Desktop.
import 'dart:convert';
import 'dart:html';
Future<void> downloadFile(String fileName, String pathToFile) async {
if (pathToFile.isEmpty) return;
if (!kIsWeb) return;
Uint8List? data = await FirebaseStorage.instance.ref(pathToFile).getData();
if (data == null) return;
String encodedData = base64Encode(data);
try {
html.AnchorElement(href: 'data:application/octet-stream;charset=utf-8;base64,$encodedData')
..setAttribute('download', fileName)
..click();
} catch (error) {
print(error);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment