Skip to content

Instantly share code, notes, and snippets.

@slightfoot
Created April 13, 2018 22:14
Show Gist options
  • Star 28 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save slightfoot/6f502205aca15e3cbf461df879673b56 to your computer and use it in GitHub Desktop.
Save slightfoot/6f502205aca15e3cbf461df879673b56 to your computer and use it in GitHub Desktop.
Download file in Dart/Flutter
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;
}
@pablo384
Copy link

Great, Thanks!

@devrnt
Copy link

devrnt commented Sep 22, 2018

Same result with package import 'package:http/http.dart' as http; (make sure you have the package defined in pubspec.yaml)

Future<File> _downloadFile(String url, String filename) async {
    http.Client client = new http.Client();
    var req = await _client.get(Uri.parse(url));
    var bytes = req.bodyBytes;
    String dir = (await getApplicationDocumentsDirectory()).path;
    File file = new File('$dir/$filename');
    await file.writeAsBytes(bytes);
    return file;
}

I suggest to make the http.Client an attribute and initialize it in the constructor.

@iamprakash13
Copy link

the code runs but does not able to locate file .. in file manager. i need permenant stored file.

@anisalibegic
Copy link

the code runs but does not able to locate file .. in file manager. i need permenant stored file.

Any solution?

@andrewackerman
Copy link

@iamprakash13 @grandpa-guru I doubt there is going to be a generalized Flutter solution to downloading a file to permanent storage as iOS doesn't really support that concept. You could write a solution that saves files to general storage on Android, but on iOS you'd have to decide how to handle that not being a real option.

@KanybekMomukeyev
Copy link

So here, don't forget to import needed packages

import 'dart:io';
import 'package:path_provider/path_provider.dart';
import 'package:flutter/foundation.dart';

@Allan-Nava
Copy link

I used the Dio package but doesn't save nothing

@slightfoot
Copy link
Author

@Allan-Nava
Copy link

thanks

@karanveerappsmaven
Copy link

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)

@oliverbytes
Copy link

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

@pishguy
Copy link

pishguy commented Aug 31, 2019

@phanirithvij
Copy link

phanirithvij commented Dec 29, 2019

This is not useful for downloading huge files, right? Because you're storing the whole file contents in the bytes variable.

@slightfoot
Copy link
Author

@phanirithvij correct. Use the flutter_downloader package above.

@chitgoks
Copy link

chitgoks commented Jan 2, 2020

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.

@Chinnu32
Copy link

how can we download the file in Flutter web application?

@levilais
Copy link

Ya - any help on downloading in Flutter Web would be awesome

@rizalfadlila
Copy link

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?

@Allan-Nava
Copy link

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

@dokinkon
Copy link

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

@devsociety-we
Copy link

Hello Guys, You can use the plugin I created to save file and download the same on web
File Saver

@seunghwanly
Copy link

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

@ahmed23423
Copy link

If, for example, I download 1 GB, it will read it all at once, and then I will write it correctly

@incrediblezayed
Copy link

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