How to Save Files from URL to Download Folder in Flutter

In this example, we are going to show you how to download any kind of file and save it to the Download folder using Flutter. You can have a progress indicator to show the progress of downloading using this example.

First, you need to add downloads_path_provider_28permission_handlerdio Flutter packages in your project by adding the following lines in pubspect.yaml file.

dependencies:
  flutter:
    sdk: flutter
  downloads_path_provider_28: ^0.1.2
  permission_handler: ^8.3.0
  dio: ^4.0.4

Add Read and Write Permission on AndroidManifest.xml file:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Also, Add this attribute on the <application> tag in AndroidManifest.xml file:

<application
   android:requestLegacyExternalStorage="true"

import 'package:dio/dio.dart';
import 'package:downloads_path_provider_28/downloads_path_provider_28.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';

void main() {
  runApp( MaterialApp(
       home: Home()
  ));
}

class Home extends  StatefulWidget {
  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {

  String fileurl = "https://fluttercampus.com/sample.pdf";
  //you can save other file formats too.
  @override
  Widget build(BuildContext context) {
    return Scaffold(
         resizeToAvoidBottomInset: false,
         appBar: AppBar(
            title: Text("Download File from URL"),
            backgroundColor: Colors.deepPurpleAccent,
         ),
          body: Container(
             margin: EdgeInsets.only(top:50),
             child: Column(
                 children: [
                      Text("File URL: $fileurl"),
                      Divider(),
                      ElevatedButton(
                         onPressed: () async {
                            Map<Permission, PermissionStatus> statuses = await [
                                Permission.storage, 
                                //add more permission to request here.
                            ].request();

                            if(statuses[Permission.storage]!.isGranted){ 
                                var dir = await DownloadsPathProvider.downloadsDirectory;
                                if(dir != null){
                                      String savename = "file.pdf";
                                      String savePath = dir.path + "/$savename";
                                      print(savePath);
                                      //output:  /storage/emulated/0/Download/banner.png

                                      try {
                                          await Dio().download(
                                              fileurl, 
                                              savePath,
                                              onReceiveProgress: (received, total) {
                                                  if (total != -1) {
                                                      print((received / total * 100).toStringAsFixed(0) + "%");
                                                      //you can build progressbar feature too
                                                  }
                                                });
                                           print("File is saved to download folder.");  
                                     } on DioError catch (e) {
                                       print(e.message);
                                     }
                                }
                            }else{
                               print("No permission to read and write.");
                            }

                         },
                         child: Text("Download File."),
                      )

                 ],
             ),
          )
      );
  }
}

Widget Output:

File Info Output:

In this way, you can download any kind of file from Network URL and save it to Download folder in the Flutter app.

No any Comments on this Article


Please Wait...