Download file from url, save to phones storage

后端 未结 4 1763
感动是毒
感动是毒 2020-12-30 06:56

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 idea

相关标签:
4条回答
  • 2020-12-30 07:32

    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)
    );
    
    0 讨论(0)
  • 2020-12-30 07:42

    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" />
    
    0 讨论(0)
  • 2020-12-30 07:47
    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;
    }
    
    0 讨论(0)
  • 2020-12-30 07:50

    FlutterChina's Dio seems like the way to go. https://github.com/flutterchina/dio/. It's robust and gives you options to retrieve the progress of your download/upload

    0 讨论(0)
提交回复
热议问题