How to Download File From URL and Save to Phone Storage In Flutter ?

3 min read
How to Download File From URL and Save to Phone Storage In Flutter
How to Download File From URL and Save to Phone Storage In Flutter

Earlier we have been through how to get Download URL From Firebase in Flutter. So in this article, we will learn How to Download File From URL and Save to Phone Storage in Flutter with a detailed article on it.

Are you ready for the same?

Get, Set, and Let’s Go.

How to Download File From URL and Save to Phone Storage In Flutter?

User can try with the available plugin flutter_downloader like below:

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)
);

If you want to Download and Save files from URL without external libraries.

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 forget to add these lines in the manifest file, otherwise,

it will not work on the real device after the build apk.

So the Users need to declare permission in an AndroidManifest.xml file using the below code snippet.

<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" />

Conclusion:

So in this article, we have been through How to Download File From URL & Save to Phone Storage In Flutter.

You can also learn about How to Delete a Firebase Storage File With Flutter and How to Upload Images to Firebase Storage In Flutter explained in a very detailed and an easy method

Thanks for being with me on a Flutter Journey.
Do share your valuable feedback for the same.

Lots of amazing and informative content coming up your way !! Stay Connected and learn 馃檪

FlutterAgency.com聽is our portal Platform dedicated to Flutter Technology and聽Flutter Developers. The portal is full of cool resources from Flutter like聽Flutter Widget聽Guide,聽Flutter Projects,聽Code libs聽and etc.

FlutterAgency.com is one of the most popular online portals dedicated to聽Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge of聽Flutter.

Leave a Reply