Last active
March 16, 2022 21:36
-
-
Save Hesamedin/d4d9c42c486b2ec8aab7e6c0771d25d1 to your computer and use it in GitHub Desktop.
This code belongs to this article: https://hesam-kamalan.medium.com/flutter-web-download-file-from-firebase-6a5c35ef7613
This file contains hidden or 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
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