Created
April 13, 2018 22:14
-
-
Save slightfoot/6f502205aca15e3cbf461df879673b56 to your computer and use it in GitHub Desktop.
Download file in Dart/Flutter
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
static var httpClient = new HttpClient(); | |
Future<File> _downloadFile(String url, String filename) async { | |
var request = await httpClient.getUrl(Uri.parse(url)); | |
var response = await request.close(); | |
var bytes = await consolidateHttpClientResponseBytes(response); | |
String dir = (await getApplicationDocumentsDirectory()).path; | |
File file = new File('$dir/$filename'); | |
await file.writeAsBytes(bytes); | |
return file; | |
} |
Hello Guys, You can use the plugin I created to save file and download the same on web
File Saver
I found a way to download files in Flutter Web using html.AnchorElement()
.
I hope this sample code would be helpful :)
sample code is downloading file from Firebase Storage
Future<void> downloadFile(String imagePath) async {
// 1) set url
String downloadURL = await firebaseStorage.FirebaseStorage.instance.ref(imagePath).getDownloadURL();
// 2) request
html.AnchorElement anchorElement = new html.AnchorElement(href: downloadURL);
anchorElement.download = downloadURL;
anchorElement.click();
}
thanks
If, for example, I download 1 GB, it will read it all at once, and then I will write it correctly
Hello, I've created a plugin for this long time ago, file_saver
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
+1. I also found it always download failed in Android release build