如何在上传数据文件之前检查数据文件的大小(Flutter)

时间:2021-04-26 07:22:59

标签: ios flutter file mobile filepath

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

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() : '...';
    });
  }

1 个答案:

答案 0 :(得分:0)

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

就像使用这个功能

var size = file.lengthSync()

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

相关问题