Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download file from url, save to phones storage

I'm working on a project, that requires me to download a file from URL, once a button is tapped, and store it to phone storage (probably downloads folder).

Any ideas on how to do this? The file that is being downloaded is also not always the same and can be anything from an image to a pdf.

like image 411
bawsi Avatar asked Jan 15 '19 10:01

bawsi


People also ask

Where does a download link file go?

You can find your downloads on your Android device in your My Files app (called File Manager on some phones), which you can find in the device's App Drawer. Unlike iPhone, app downloads are not stored on the home screen of your Android device, and can be found with an upward swipe on the home screen.

Where do download link files go on iPhone?

iOS and iPad. iPhone and iPad both have an app called Files where you can locate all of your files from services including iCloud Drive, Dropbox, and more. If you want to access the Downloads folder, you can find it inside the Files app.

How do I download from URL and save in local storage in flutter?

To download the file and store it in the download folder using flutter we need to use files_utils and path_provider Plugin in our App. This will provide us to store files into our sdcard/downloads folder and then we can use flutter_downloader OR dio plugin to download file and then we can save it in our specific path.


2 Answers

Use https://pub.dartlang.org/packages/flutter_downloader. Don't forget to do platform configurations.

Basically, this is how you should use the package. There is a detailed long example in the link.

final taskId = await FlutterDownloader.enqueue(   url: 'your download link',   savedDir: 'the path of directory where you want to save downloaded files',   showNotification: true, // show download progress in status bar (for Android)   openFileFromNotification: true, // click on notification to open downloaded file (for Android) ); 

Edit: Some people said the package on top is to well maintained. Try this one https://pub.dev/packages/flowder

like image 139
westdabestdb Avatar answered Nov 09 '22 00:11

westdabestdb


If you want to download and save file from URL without external libraries, you can use this code. It works for me, but I do not check it on big files. Good luck.

Future<String> downloadFile(String url, String fileName, String dir) async {         HttpClient httpClient = new HttpClient();         File file;         String filePath = '';         String myUrl = '';              try {           myUrl = url+'/'+fileName;           var request = await httpClient.getUrl(Uri.parse(myUrl));           var response = await request.close();           if(response.statusCode == 200) {             var bytes = await consolidateHttpClientResponseBytes(response);             filePath = '$dir/$fileName';             file = File(filePath);             await file.writeAsBytes(bytes);           }           else             filePath = 'Error code: '+response.statusCode.toString();         }         catch(ex){           filePath = 'Can not fetch url';         }              return filePath;       } 

For Android do not forgetto add these lines in the manifest file, otherwise it will not work on real device after build apk:

<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
like image 29
Murat Kurbanov Avatar answered Nov 09 '22 02:11

Murat Kurbanov