【问题标题】:How do I check the size of the data file before I upload it (Flutter)在上传之前如何检查数据文件的大小(Flutter)
【发布时间】:2021-07-19 13:52:30
【问题描述】:

当我取文件时我想要数据文件大小信息,以后将根据文件大小进行上传文件限制。我使用的以下代码

void _openFileExplorer() async {
    setState(() => _loadingPath = true);
    try {
      _directoryPath = null;
      _paths = (await FilePicker.platform.pickFiles(
        type: _pickingType,
        allowMultiple: _multiPick,
        allowedExtensions: ['jpg', 'pdf', 'doc', 'docx', 'png', 'jpeg'],
      ))
          ?.files;
    } on PlatformException catch (e) {
      print("Unsupported operation" + e.toString());
    } catch (ex) {
      print(ex);
    }
    if (!mounted) return;
    setState(() {
      _loadingPath = false;
      _fileName = _paths != null ? _paths.map((e) => e.name).toString() : '...';
    });
  }

【问题讨论】:

    标签: ios flutter file mobile filepath


    【解决方案1】:

    您可以使用名为 lengthSync 的函数获取文件大小。

    像这样使用这个函数

    var size = file.lengthSync()
    

    它将以字节为单位给出文件大小。

    【讨论】:

    • 我需要为 iOS 做权限吗?我试了代码,出现的信息如下: Unhandled Exception: FileSystemException: Cannot retrieve length of file, path = '(20201223160946.pdf) '
    • 你需要为文件提供通用路径。您在需要提供完整路径来获取文件的路径中仅使用文件名。
    • 您可以使用资产文件进行测试
    猜你喜欢
    • 2019-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-07
    • 1970-01-01
    • 1970-01-01
    • 2020-12-20
    • 2016-06-17
    相关资源
    最近更新 更多