Skip to content
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

feat: FilePicker.upload() supports modifying filenames before upload #5045

Merged
merged 3 commits into from
Mar 31, 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
63 changes: 45 additions & 18 deletions packages/flet/lib/src/controls/file_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import '../flet_app_services.dart';
import '../flet_control_backend.dart';
import '../flet_server.dart';
import '../models/control.dart';
import '../utils/numbers.dart';
import '../utils/platform.dart';
import '../utils/strings.dart';
import 'flet_store_mixin.dart';
Expand All @@ -27,23 +28,32 @@ class FilePickerResultEvent {
}

class FilePickerFile {
final int id;
final String name;
final String? path;
final int size;

FilePickerFile({required this.name, required this.path, required this.size});
FilePickerFile(
{required this.id,
required this.name,
required this.path,
required this.size});

Map<String, dynamic> toJson() =>
<String, dynamic>{'name': name, 'path': path, 'size': size};
<String, dynamic>{'id': id, 'name': name, 'path': path, 'size': size};
}

class FilePickerUploadFile {
final int id;
final String name;
final String uploadUrl;
final String method;

FilePickerUploadFile(
{required this.name, required this.uploadUrl, required this.method});
{required this.id,
required this.name,
required this.uploadUrl,
required this.method});
}

class FilePickerUploadProgressEvent {
Expand Down Expand Up @@ -119,17 +129,23 @@ class _FilePickerControlState extends State<FilePickerControl>
!isDesktopPlatform()) {
resetDialogState();
}

widget.backend.triggerControlEvent(
widget.control.id,
"result",
json.encode(FilePickerResultEvent(
path: _path,
files: _files
?.map((f) => FilePickerFile(
name: f.name,
path: kIsWeb ? null : f.path,
size: f.size))
.toList())));
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(),
)),
);
}

if (_state != state) {
Expand All @@ -154,7 +170,7 @@ class _FilePickerControlState extends State<FilePickerControl>
allowMultiple: allowMultiple,
withData: false,
withReadStream: true)
.then((result) {
.then((FilePickerResult? result) {
debugPrint("pickFiles() completed");
_files = result?.files;
sendEvent();
Expand All @@ -177,7 +193,7 @@ class _FilePickerControlState extends State<FilePickerControl>
sendEvent();
});
}
// saveFile
// getDirectoryPath
else if (state?.toLowerCase() == "getdirectorypath" && !kIsWeb) {
FilePicker.platform
.getDirectoryPath(
Expand Down Expand Up @@ -207,17 +223,28 @@ class _FilePickerControlState extends State<FilePickerControl>
Future uploadFiles(String filesJson, FletServer server, Uri pageUri) async {
var uj = json.decode(filesJson);
var uploadFiles = (uj as List).map((u) => FilePickerUploadFile(
name: u["name"], uploadUrl: u["upload_url"], method: u["method"]));
id: parseInt(u["id"], -1)!, // -1 = invalid
name: u["name"],
uploadUrl: u["upload_url"],
method: u["method"]));

for (var uf in uploadFiles) {
var file = _files!.firstWhereOrNull((f) => f.name == uf.name);
var file = ((uf.id >= 0 && uf.id < _files!.length)
? _files![uf.id]
: null) // by id
??
_files!.firstWhereOrNull((f) => f.name == uf.name); // by name

if (file != null) {
try {
await uploadFile(
file, server, getFullUploadUrl(pageUri, uf.uploadUrl), uf.method);
_files!.remove(file);
_files!.remove(file); // Remove the uploaded file
} catch (e) {
sendProgress(server, file.name, null, e.toString());
}
} else {
debugPrint("Error: File '${uf.name}' (id: ${uf.id}) not found.");
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions sdk/python/packages/flet/src/flet/core/file_picker.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class FilePickerFileType(Enum):
class FilePickerUploadFile:
name: str
upload_url: str
id: int = None
method: str = field(default="PUT")


Expand All @@ -42,6 +43,7 @@ class FilePickerFile:
name: str
path: str
size: int
id: int


class FilePickerResultEvent(ControlEvent):
Expand Down