fileUpload method

Future<void> fileUpload(
  1. dynamic context,
  2. ValueChanged<UploadResultEntity> changed,
  3. String parentId,
  4. IPFSFileType fileType, {
  5. Function? progressCallback,
  6. List<String>? allowedExtensions,
})

文件上传

Implementation

Future<void> fileUpload(context, ValueChanged<UploadResultEntity> changed,
    String parentId, IPFSFileType fileType,
    {Function? progressCallback, List<String>? allowedExtensions}) async {
  FilePickerResult? result;
  print(allowedExtensions);
  switch (fileType) {
    case IPFSFileType.any:
      result = await FilePicker.platform.pickFiles(type: FileType.any);
      break;
    case IPFSFileType.video:
      result = await FilePicker.platform.pickFiles(type: FileType.video);
      break;
    case IPFSFileType.image:
      result = await FilePicker.platform.pickFiles(type: FileType.image);
      break;
    case IPFSFileType.audio:
      result = await FilePicker.platform.pickFiles(type: FileType.audio);
      break;
    case IPFSFileType.media:
    case IPFSFileType.imageAndVideo:
      result = await FilePicker.platform.pickFiles(type: FileType.media);
      break;
    case IPFSFileType.custom:
      result = await FilePicker.platform.pickFiles(
          type: FileType.custom, allowedExtensions: allowedExtensions);
      break;
  }

  if (result != null &&
      result.files.first != null &&
      result.files.first.extension != null &&
      result.files.first.extension!.isNotEmpty) {
    PlatformFile file = result.files.first;
    if (file != null) {
      String filePath = file.path ?? '';
      if (filePath.isEmpty) {
        ToastUtils.show('选择的文件不存在,请重新选择');
        return;
      }
      // int fileSize = await file.length();
      // if (fileSize > 40 * 1024 * 1024) {
      //   ToastUtils.show('文件大小超出限制');
      //   return;
      // }

      voneFileUpload(context, filePath, changed, parentId,
          progressCallback: progressCallback);
    } else {
      ToastUtils.show('选择的文件不存在,请重新选择');
    }
  }
}