ios - 如何在 iOS 中下载和打开文件?
问题描述
我想下载一个文件然后打开它。它在:Android iOS 模拟器上运行良好但在 iPhone 上我得到了例外
文件系统异常:无法创建文件,路径 = 'var/mobile/Container/Data/Application/{ID}/Documents2743.pdf'(操作系统错误:不允许操作,errno = 1)
我已经写了下面的代码。
PermissionStatus _permissionStatus = await PermissionHandler()
.checkPermissionStatus(PermissionGroup.storage);
if (_permissionStatus == PermissionStatus.granted) {
FileUtils.mkdir(['/sdcard/h8subscriber/']);
FileUtils.mkdir(['/sdcard/h8subscriber/documents/']);
if (Platform.isAndroid) {
dirloc = "/sdcard/h8subscriber/documents/";
} else {
dirloc = (await getApplicationDocumentsDirectory()).path;
}
Dio dio = Dio();
var randid = random.nextInt(10000);
try {
FileUtils.mkdir([dirloc]);
await dio.download(fileURL, dirloc + randid.toString() + ".pdf",
onReceiveProgress: (receivedBytes, totalBytes) {
setState(() {
downloading = true;
progress =
((receivedBytes / totalBytes) * 100).toStringAsFixed(0) + "%";
});
print("downloading");
});
} catch (e) {
print(e);
}
setState(() {
downloading = false;
progress = "";
path = dirloc + randid.toString() + ".pdf";
_isLoading = false;
showAlertOk(
'Download Successfull',
'Your downloaded file is here..\n'
'$path',
path);
});
} else if (_permissionStatus == PermissionStatus.restricted) {
bool isOpened = await PermissionHandler().openAppSettings();
showToast(context, 'Permission Denied!');
} else if (_permissionStatus == PermissionStatus.unknown) {
dirloc = (await getApplicationDocumentsDirectory()).path;
Dio dio = Dio();
var randid = random.nextInt(10000);
try {
FileUtils.mkdir([dirloc]);
await dio.download(fileURL, dirloc + randid.toString() + ".pdf",
onReceiveProgress: (receivedBytes, totalBytes) {
setState(() {
downloading = true;
progress =
((receivedBytes / totalBytes) * 100).toStringAsFixed(0) + "%";
});
});
} catch (e) {
print(e);
}
setState(() {
downloading = false;
progress = "";
path = dirloc + randid.toString() + ".pdf";
_isLoading = false;
showAlertOk(
'Download Successfull',
'Your downloaded file is here..\n'
'$path',
path);
});
} else {
showToast(context, 'Permission Denied!');
setState(() {});
}
解决方案
好的,我不知道这是否是预期的结果,但是我可以使用 getTempDirectory() 而不是 getApplicationDocumentsDirectory() 来下载文件
推荐阅读
- c# - 如何从文本框或富文本框或 ETC 中获取“组分隔符”、0x1D 字符。C#
- mongodb - MongoDB 聚合管道的操作超时
- r - 创建一个函数来循环数据帧以在 R 中创建显着相关性的分布
- javascript - 未捕获的错误:不匹配的匿名定义():使用汇总 [UMD] 构建时:如何解决?
- caching - 如何计算各种类型缓存的 TTL?
- java - Spring 不会跟踪存储在“./resources/”文件夹中的文件的更改
- html - 为什么这个要点中的文字没有对齐?
- database - 无法打开 access 数据库 2003(因为以前的 access 版本)
- python - 使用 for 循环读取 json dict 并写入 pandas 数据帧
- c - 接收部分在 TCP 套接字编程中不能正常工作