简体   繁体   中英

Download file to Device download folder in Flutter

I'm trying to download files in my flutter app using the dio plugin for which I have to specify a "savePath". But using path_provider I can only see ApplicationDocumentsDirectory, ExternalStorageDirectory etc. My idea is to simply show the file(pdf) in the device's Download Folder, so whenever the user wants to view the file, he just goes to the folder and clicks it. Saving to ApplicationDocumentsDirectory (as I understood it) would require me to provide a link (of sorts) to the file in the app, meaning the user would have to open my app every time he wants to view files downloaded through my app.

I just want the files to be available in the DownLoads Folder, just like with so many files we download directly to the DownLoads Folder.

Am I missing something here? pls enlighten me!

Added this code on request from user in comments:

 var dir; Future < dynamic > downloadFile(url, filename) async { Dio dio = Dio(); try { // checkPermission(); //if(Externa) dir = await getApplicationDocumentsDirectory(); if (dir.path != null) print("url:$url"); print("path:${dir.path}/$filename"); if (await File("${dir.path}/$filename").exists()) { setState(() { filePath = "${dir.path}/$filename"; }); return; } await dio.download(url, "${dir.path}/$filename", onReceiveProgress: (rec, total) { print("Rec: $rec , Total: $total"); setState(() { downloading = true; progressString = ((rec / total) * 100).toStringAsFixed(0) + "%"; }); }); } catch (e) { print("downloadErr:$e"); } setState(() { downloading = false; progressString = "Concluido"; filePath = "${dir.path}/$filename"; }); }

You can use downloads_path_provider for it

https://pub.dev/packages/downloads_path_provider

import 'package:downloads_path_provider/downloads_path_provider.dart';  

Future<Directory> downloadsDirectory = DownloadsPathProvider.downloadsDirectory;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM