-
-
Save slightfoot/6f502205aca15e3cbf461df879673b56 to your computer and use it in GitHub Desktop.
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; | |
} |
thanks
on Android you can use https://pub.dev/documentation/path_provider/latest/path_provider/getExternalStorageDirectory.html
I am using the above code to download and store file locally but I am not able to find file downloaded in storage. Where is it located. I have searched through all folders(in android)
on Android you can use https://pub.dev/documentation/path_provider/latest/path_provider/getExternalStorageDirectory.html
I am using the above code to download and store file locally but I am not able to find file downloaded in storage. Where is it located. I have searched through all folders(in android)
I believe you if you use path_provider 0.5.0+1 below you'll get it working... latest versions broke it and just saves to app/data directory... use this alternative to download to /Downloads directory https://pub.dev/packages/downloads_path_provider
This is not useful for downloading huge files, right? Because you're storing the whole file contents in the bytes
variable.
@phanirithvij correct. Use the flutter_downloader
package above.
downloads_path_provider
i would not suggest downloads_path_provider because its build.gradle is set to compileSdkVersion 27 which results in build failures when creating signed apks. the issue page also is disabled so nobody could create a new issue and inform the author about it and it is currently archived since he said it has a lot of inconsistencies.
how can we download the file in Flutter web application?
Ya - any help on downloading in Flutter Web would be awesome
downloads_path_provider
i would not suggest downloads_path_provider because its build.gradle is set to compileSdkVersion 27 which results in build failures when creating signed apks. the issue page also is disabled so nobody could create a new issue and inform the author about it and it is currently archived since he said it has a lot of inconsistencies.
any solution?
downloads_path_provider
i would not suggest downloads_path_provider because its build.gradle is set to compileSdkVersion 27 which results in build failures when creating signed apks. the issue page also is disabled so nobody could create a new issue and inform the author about it and it is currently archived since he said it has a lot of inconsistencies.
any solution?
+1
downloads_path_provider
i would not suggest downloads_path_provider because its build.gradle is set to compileSdkVersion 27 which results in build failures when creating signed apks. the issue page also is disabled so nobody could create a new issue and inform the author about it and it is currently archived since he said it has a lot of inconsistencies.
+1. I also found it always download failed in Android release build
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
Hello, I've created a plugin for this long time ago, file_saver
Future saveLocalFile(BuildContext context, String localFilePath) async {
try {
showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return Center(
child: CircularProgressIndicator(),
);
},
);
String filePath = localFilePath;
File file = File(filePath);
if (await file.exists()) {
Directory? appDocDir = await getExternalStorageDirectory();
if (appDocDir != null) {
String destPath = "${appDocDir.path}/${file.path.split('/').last}";
await file.copy(destPath);
String mimeType = _getMimeType(destPath);
if (Platform.isAndroid) {
final androidFile = File(destPath);
await androidFile.writeAsBytes(await file.readAsBytes(),
flush: true);
await androidFile.uri;
}
Navigator.pop(context);
showMessage('File saved successfully', Colors.green);
} else {
throw 'Failed to get directory for saving file';
}
} else {
throw 'File not found at path: $filePath';
}
} catch (e) {
Navigator.pop(context);
showMessage('Failed to save file: $e', Colors.red);
}
}
file downloading but not opening , please give me a solution
on Android you can use https://pub.dev/documentation/path_provider/latest/path_provider/getExternalStorageDirectory.html