【问题标题】:How to Download and open file in iOS in flutter?如何在 iOS 中下载和打开文件?
【发布时间】:2021-04-23 11:37:07
【问题描述】:

我想下载一个文件然后打开它。 它在以下方面运行良好: 安卓 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(() {});
}

【问题讨论】:

    标签: ios iphone flutter


    【解决方案1】:

    好的,我不知道这是否是预期的结果,但我可以使用 getTempDirectory() 而不是 getApplicationDocumentsDirectory() 来下载文件

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-01
      • 2018-03-14
      相关资源
      最近更新 更多