pickMultipleFiles method

  1. @override
Future<List<String>?> pickMultipleFiles(
  1. MediaOptions options,
  2. List<String>? allowedExtensions
)
override

Implementation

@override
Future<List<String>?> pickMultipleFiles(
    MediaOptions options, List<String>? allowedExtensions) async {
  final completer = Completer<List<String>?>();
  final input = web.document.createElement('input') as web.HTMLInputElement;
  input.type = 'file';
  input.multiple = true;
  input.style.display = 'none';
  if (allowedExtensions != null && allowedExtensions.isNotEmpty) {
    input.accept =
        allowedExtensions.map((e) => e.startsWith('.') ? e : '.$e').join(',');
  } else {
    input.accept = '*/*';
  }
  input.addEventListener(
      'change',
      (web.Event event) {
        final files = input.files;
        if (files != null && files.length > 0) {
          final urls = <String>[];
          for (var i = 0; i < files.length; i++) {
            final file = files.item(i);
            if (file != null) {
              try {
                // Check if it's a video file based on accept attribute
                if (input.accept.contains('video/')) {
                  urls.add(_createVideoObjectURL(file));
                } else {
                  urls.add(web.URL.createObjectURL(file));
                }
              } catch (e) {
                _log(
                    'Failed to create object URL for file: ${file.name}, error: $e');
                // Skip this file but continue with others
              }
            }
          }
          completer.complete(urls);
        } else {
          completer.complete(null);
        }
      }.toJS);
  if (web.document.body != null) {
    web.document.body!.appendChild(input);
  }
  input.click();
  input.remove();
  return completer.future;
}