Skip to content

v0: Android and iOS: allow passing src_bytes in FilePicker.save_file() call #5378

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions client/macos/Runner/DebugProfile.entitlements
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.app-sandbox</key>
<false/>
<key>com.apple.security.cs.allow-jit</key>
<true/>
<key>com.apple.security.network.server</key>
<true/>
<key>com.apple.security.network.client</key>
<true/>
<key>com.apple.security.device.audio-input</key>
<true/>
<key>com.apple.security.personal-information.location</key>
<true/>
</dict>
</plist>
<dict>
<key>com.apple.security.app-sandbox</key>
<false />
<key>com.apple.security.cs.allow-jit</key>
<true />
<key>com.apple.security.network.server</key>
<true />
<key>com.apple.security.network.client</key>
<true />
<key>com.apple.security.device.audio-input</key>
<true />
<key>com.apple.security.personal-information.location</key>
<true />
<key>com.apple.security.files.user-selected.read-write</key>
<true />
</dict>
</plist>
24 changes: 13 additions & 11 deletions client/macos/Runner/Release.entitlements
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.app-sandbox</key>
<false/>
<key>com.apple.security.network.client</key>
<true/>
<key>com.apple.security.device.audio-input</key>
<true/>
<key>com.apple.security.personal-information.location</key>
<true/>
</dict>
</plist>
<dict>
<key>com.apple.security.app-sandbox</key>
<false />
<key>com.apple.security.network.client</key>
<true />
<key>com.apple.security.device.audio-input</key>
<true />
<key>com.apple.security.personal-information.location</key>
<true />
<key>com.apple.security.files.user-selected.read-write</key>
<true />
</dict>
</plist>
73 changes: 43 additions & 30 deletions packages/flet/lib/src/controls/file_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ import 'flet_store_mixin.dart';
class FilePickerResultEvent {
final String? path;
final List<FilePickerFile>? files;
final String? error;

FilePickerResultEvent({required this.path, required this.files});
FilePickerResultEvent(
{required this.path, required this.files, required this.error});

Map<String, dynamic> toJson() => <String, dynamic>{
'path': path,
'files': files?.map((f) => f.toJson()).toList()
'files': files?.map((f) => f.toJson()).toList(),
'error': error
};
}

Expand Down Expand Up @@ -94,6 +97,7 @@ class _FilePickerControlState extends State<FilePickerControl>
String? _upload;
String? _path;
List<PlatformFile>? _files;
String? _error;

@override
Widget build(BuildContext context) {
Expand All @@ -108,6 +112,7 @@ class _FilePickerControlState extends State<FilePickerControl>
var allowMultiple = widget.control.attrBool("allowMultiple", false)!;
var allowedExtensions =
parseStringList(widget.control, "allowedExtensions");
var srcBytesBase64 = widget.control.attrString("srcBytes");
FileType fileType = FileType.values.firstWhere(
(m) =>
m.name.toLowerCase() ==
Expand All @@ -134,23 +139,24 @@ class _FilePickerControlState extends State<FilePickerControl>
widget.control.id,
"result",
json.encode(FilePickerResultEvent(
path: _path,
files: _files?.asMap().entries.map((entry) {
PlatformFile f = entry.value;
return FilePickerFile(
id: entry.key, // use entry's index as id
name: f.name,
path: kIsWeb ? null : f.path,
size: f.size,
);
}).toList(),
)),
path: _path,
files: _files?.asMap().entries.map((entry) {
PlatformFile f = entry.value;
return FilePickerFile(
id: entry.key, // use entry's index as id
name: f.name,
path: kIsWeb ? null : f.path,
size: f.size,
);
}).toList(),
error: _error)),
);
}

if (_state != state) {
_path = null;
_files = null;
_error = null;
_state = state;

if (isDesktopPlatform() &&
Expand Down Expand Up @@ -178,24 +184,31 @@ class _FilePickerControlState extends State<FilePickerControl>
}
// saveFile
else if (state?.toLowerCase() == "savefile" && !kIsWeb) {
FilePicker.platform
.saveFile(
dialogTitle: dialogTitle,
fileName: fileName != null || !isiOSPlatform()
? fileName
: "new-file",
initialDirectory: initialDirectory,
lockParentWindow: true,
type: fileType,
allowedExtensions: allowedExtensions,
bytes: isAndroidPlatform() || isiOSPlatform()
? Uint8List(0)
: null)
.then((result) {
debugPrint("saveFile() completed");
_path = result;
if ((isAndroidPlatform() || isiOSPlatform()) &&
srcBytesBase64 == null) {
_error =
'"src_bytes" is required on Android & iOS when saving a file.';
sendEvent();
});
} else {
FilePicker.platform
.saveFile(
dialogTitle: dialogTitle,
fileName: fileName != null || !isiOSPlatform()
? fileName
: "new-file",
initialDirectory: initialDirectory,
lockParentWindow: true,
type: fileType,
allowedExtensions: allowedExtensions,
bytes: srcBytesBase64 != null
? base64Decode(srcBytesBase64)
: null)
.then((result) {
debugPrint("saveFile() completed");
_path = result;
sendEvent();
});
}
}
// getDirectoryPath
else if (state?.toLowerCase() == "getdirectorypath" && !kIsWeb) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,7 @@ def setup_template_data(self):
"com.apple.security.cs.allow-jit": True,
"com.apple.security.network.client": True,
"com.apple.security.network.server": True,
"com.apple.security.files.user-selected.read-write": True,
}
android_permissions = {"android.permission.INTERNET": True}
android_features = {
Expand Down
16 changes: 16 additions & 0 deletions sdk/python/packages/flet/src/flet/core/file_picker.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import base64
import json
from dataclasses import dataclass, field
from enum import Enum
Expand Down Expand Up @@ -45,6 +46,7 @@ class FilePickerResultEvent(ControlEvent):
def __init__(self, e: ControlEvent):
super().__init__(e.target, e.name, e.data, e.control, e.page)
d = json.loads(e.data)
self.error = d.get("error")
self.path: Optional[str] = d.get("path")
self.files: Optional[List[FilePickerFile]] = None
files = d.get("files")
Expand Down Expand Up @@ -173,13 +175,15 @@ def save_file(
initial_directory: Optional[str] = None,
file_type: FilePickerFileType = FilePickerFileType.ANY,
allowed_extensions: Optional[List[str]] = None,
src_bytes: Optional[bytes] = None,
):
self.state = FilePickerState.SAVE_FILE
self.dialog_title = dialog_title
self.file_name = file_name
self.initial_directory = initial_directory
self.file_type = file_type
self.allowed_extensions = allowed_extensions
self.src_bytes = src_bytes
self.update()

def get_directory_path(
Expand Down Expand Up @@ -266,6 +270,18 @@ def allow_multiple(self) -> bool:
def allow_multiple(self, value: Optional[bool]):
self._set_attr("allowMultiple", value)

# src_bytes
@property
def src_bytes(self) -> Optional[bytes]:
v = self._get_attr("srcBytes")
return base64.b64decode(v) if v else v

@src_bytes.setter
def src_bytes(self, value: Optional[bytes]):
self._set_attr(
"srcBytes", base64.b64encode(value).decode("utf-8") if value else value
)

# on_result
@property
def on_result(self) -> OptionalEventCallable[FilePickerResultEvent]:
Expand Down