Skip to content

Commit

Permalink
Merge pull request #1665 from vicajilau/feature/path-blob-on-web
Browse files Browse the repository at this point in the history
Add Blob Creation as Path in flutter_file_picker
  • Loading branch information
navaronbracke authored Feb 15, 2025
2 parents 7146e85 + 7fcc437 commit 70db27b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 9.0.0
### Web
- **BREAKING CHANGE:** `pickFiles()` now loads files as blobs. See the note in the updated [wiki](https://github.com/miguelpruivo/flutter_file_picker/wiki/api#-pickfiles)

## 8.3.7
### Android
- Updated example project Android sources. [@vicajilau](https://github.com/vicajilau)
Expand Down
11 changes: 9 additions & 2 deletions lib/_internal/file_picker_web.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'dart:async';
import 'dart:js_interop';
import 'package:web/web.dart';
import 'dart:typed_data';

import 'package:file_picker/file_picker.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'package:web/web.dart';

class FilePickerWeb extends FilePicker {
late Element _target;
Expand Down Expand Up @@ -86,9 +86,16 @@ class FilePickerWeb extends FilePicker {
String? path,
Stream<List<int>>? readStream,
) {
String? blobUrl;
if (bytes != null && bytes.isNotEmpty) {
final blob =
Blob([bytes.toJS].toJS, BlobPropertyBag(type: file.type));

blobUrl = URL.createObjectURL(blob);
}
pickedFiles.add(PlatformFile(
name: file.name,
path: path,
path: path ?? blobUrl,
size: bytes != null ? bytes.length : file.size,
bytes: bytes,
readStream: readStream,
Expand Down
22 changes: 5 additions & 17 deletions lib/src/platform_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import 'package:flutter/foundation.dart';

class PlatformFile {
PlatformFile({
String? path,
this.path,
required this.name,
required this.size,
this.bytes,
this.readStream,
this.identifier,
}) : _path = path;
});

factory PlatformFile.fromMap(Map data, {Stream<List<int>>? readStream}) {
return PlatformFile(
Expand All @@ -31,21 +31,9 @@ class PlatformFile {
/// ```
/// final File myFile = File(platformFile.path);
/// ```
/// On web this is always `null`. You should access `bytes` property instead.
/// On web the path points to a Blob URL, if present, which can be cleaned up using [URL.revokeObjectURL].
/// Read more about it [here](https://github.com/miguelpruivo/flutter_file_picker/wiki/FAQ)
String? _path;

String? get path {
if (kIsWeb) {
/// https://github.com/miguelpruivo/flutter_file_picker/issues/751
throw '''
On web `path` is unavailable and accessing it causes this exception.
You should access `bytes` property instead,
Read more about it [here](https://github.com/miguelpruivo/flutter_file_picker/wiki/FAQ)
''';
}
return _path;
}
final String? path;

/// File name including its extension.
final String name;
Expand Down Expand Up @@ -89,7 +77,7 @@ class PlatformFile {
}

return other is PlatformFile &&
(kIsWeb || other.path == path) &&
other.path == path &&
other.name == name &&
other.bytes == bytes &&
other.readStream == readStream &&
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: A package that allows you to use a native file explorer to pick sin
homepage: https://github.com/miguelpruivo/plugins_flutter_file_picker
repository: https://github.com/miguelpruivo/flutter_file_picker
issue_tracker: https://github.com/miguelpruivo/flutter_file_picker/issues
version: 8.3.7
version: 9.0.0

dependencies:
flutter:
Expand Down

0 comments on commit 70db27b

Please sign in to comment.