【发布时间】:2020-04-24 09:12:13
【问题描述】:
我正在尝试使用必须指定“savePath”的 dio 插件在我的颤振应用程序中下载文件。但是使用 path_provider 我只能看到 ApplicationDocumentsDirectory、ExternalStorageDirectory 等。我的想法是在设备的下载文件夹中简单地显示文件(pdf),所以每当用户想要查看文件时,他只需转到该文件夹并单击它。 保存到 ApplicationDocumentsDirectory(据我所知)需要我提供指向应用程序中文件的链接(各种),这意味着用户每次想要查看通过我的应用程序下载的文件时都必须打开我的应用程序。
我只希望文件在 DownLoads 文件夹中可用,就像我们直接下载到 DownLoads 文件夹中的许多文件一样。
我在这里遗漏了什么吗?请赐教!
根据用户要求在 cmets 中添加此代码:
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";
});
}
【问题讨论】:
-
您好,我也遇到了和您一样的问题,请问您找到解决方法了吗?我尝试过使用
downloads_path_provider,但是在我看到下面的 cmets 之后......这个包不再工作了 -
@uyhaW,我在原始问题中添加了一些代码。看看它是否适合你。