如何在 flutter 中下载并打开 iOS 中的文件?
How to Download and open file in iOS in flutter?
我想下载一个文件然后打开它。
它在以下方面工作正常:
Android
iOS模拟器
但是在 iPhone 我得到了异常
Filesystem exception : Cannot create file, path = 'var/mobile/Container/Data/Application/{ID}/Documents2743.pdf' (OS Error: Operation not permitted, 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() 下载文件
我想下载一个文件然后打开它。 它在以下方面工作正常: Android iOS模拟器 但是在 iPhone 我得到了异常
Filesystem exception : Cannot create file, path = 'var/mobile/Container/Data/Application/{ID}/Documents2743.pdf' (OS Error: Operation not permitted, 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() 下载文件