Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download a file and store it in Downloads folder using Flutter

Tags:

flutter

I'm building an App using Flutter 1.10.3 and I'm having difficulties downloading images to the Downloads folder of the device.

Is there an option to do so without using any 3rd-part libraries?

like image 485
Eliya Cohen Avatar asked Sep 18 '19 14:09

Eliya Cohen


People also ask

How do I save downloads folder in flutter?

Show activity on this post. then use these 2 packages: permission_handler to request storage permission. downloads_path_provider to save the file in downloads directory.

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

If you control the server, another option is to let the user's system handle the download. On the server you add a Content-Disposition: attachment header to the response. In nginx you can do this with the add_header directive in your sites-enabled config file:

add_header Content-Disposition "attachment";

More notes about that here.

Then on the Flutter side you can use the url_launcher package to initiate the download:

await launch('https://www.example.com/my_file.pdf');

This will hand over the downloading to the browser installed on the phone. And the Content-Disposition: attachment header will tell the browser that it is to be downloaded instead of displayed or played.

This solution is pretty painless, though it doesn't give you control about where the file is downloaded to.

like image 110
Suragch Avatar answered Nov 01 '22 10:11

Suragch


Add few required dependencies, I have a test demo with android only. please review file_utils and path_provider while you implement in IOS device.

  dio: ^3.0.0

  path_provider: ^1.3.0

  simple_permissions: ^0.1.9

  file_utils: ^0.1.3

Note: simple_permissions is disconnected please use this dependency permission_handler

You are required to add permissions on the android manifest file.

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

Sample code:

  import 'dart:io';
  import 'package:flutter/material.dart';
  import 'package:dio/dio.dart';
  import 'package:path_provider/path_provider.dart';
  import 'dart:async';
  import 'package:simple_permissions/simple_permissions.dart';
  import 'package:file_utils/file_utils.dart';
  import 'dart:math';

  void main() => runApp(Downloader());

  class Downloader extends StatelessWidget {
    @override
    Widget build(BuildContext context) => MaterialApp(
          title: "File Downloader",
          debugShowCheckedModeBanner: false,
          home: FileDownloader(),
          theme: ThemeData(primarySwatch: Colors.blue),
        );
  }

  class FileDownloader extends StatefulWidget {
    @override
    _FileDownloaderState createState() => _FileDownloaderState();
  }

  class _FileDownloaderState extends State<FileDownloader> {

    final imgUrl = "https://images6.alphacoders.com/683/thumb-1920-683023.jpg";
    bool downloading = false;
    var progress = "";
    var path = "No Data";
    var platformVersion = "Unknown";
    Permission permission1 = Permission.WriteExternalStorage;
    var _onPressed;
    static final Random random = Random();
    Directory externalDir;

    @override
    void initState() {
      super.initState();
      downloadFile();
    }


    Future<void> downloadFile() async {
      Dio dio = Dio();
      bool checkPermission1 =
          await SimplePermissions.checkPermission(permission1);
      // print(checkPermission1);
      if (checkPermission1 == false) {
        await SimplePermissions.requestPermission(permission1);
        checkPermission1 = await SimplePermissions.checkPermission(permission1);
      }
      if (checkPermission1 == true) {
        String dirloc = "";
        if (Platform.isAndroid) {
          dirloc = "/sdcard/download/";
        } else {
          dirloc = (await getApplicationDocumentsDirectory()).path;
        }

        var randid = random.nextInt(10000);

        try {
          FileUtils.mkdir([dirloc]);
          await dio.download(imgUrl, dirloc + randid.toString() + ".jpg",
              onReceiveProgress: (receivedBytes, totalBytes) {
            setState(() {
              downloading = true;
              progress =
                  ((receivedBytes / totalBytes) * 100).toStringAsFixed(0) + "%";
            });
          });
        } catch (e) {
          print(e);
        }

        setState(() {
          downloading = false;
          progress = "Download Completed.";
          path = dirloc + randid.toString() + ".jpg";
        });
      } else {
        setState(() {
          progress = "Permission Denied!";
          _onPressed = () {
            downloadFile();
          };
        });
      }
    }

    @override
    Widget build(BuildContext context) => Scaffold(
        appBar: AppBar(
          title: Text('File Downloader'),
        ),
        body: Center(
            child: downloading
                ? Container(
                    height: 120.0,
                    width: 200.0,
                    child: Card(
                      color: Colors.black,
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
                          CircularProgressIndicator(),
                          SizedBox(
                            height: 10.0,
                          ),
                          Text(
                            'Downloading File: $progress',
                            style: TextStyle(color: Colors.white),
                          ),
                        ],
                      ),
                    ),
                  )
                : Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      Text(path),
                      MaterialButton(
                        child: Text('Request Permission Again.'),
                        onPressed: _onPressed,
                        disabledColor: Colors.blueGrey,
                        color: Colors.pink,
                        textColor: Colors.white,
                        height: 40.0,
                        minWidth: 100.0,
                      ),
                    ],
                  )));
  }

enter image description here

like image 37
Amit Prajapati Avatar answered Nov 01 '22 09:11

Amit Prajapati