Skip to content

Commit c8c2649

Browse files
authored
feat: FilePicker.upload() supports modifying filenames before upload (#5045)
* initial commit * add a comment * update debug message
1 parent 92c2b1b commit c8c2649

File tree

2 files changed

+47
-18
lines changed

2 files changed

+47
-18
lines changed

packages/flet/lib/src/controls/file_picker.dart

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import '../flet_app_services.dart';
1010
import '../flet_control_backend.dart';
1111
import '../flet_server.dart';
1212
import '../models/control.dart';
13+
import '../utils/numbers.dart';
1314
import '../utils/platform.dart';
1415
import '../utils/strings.dart';
1516
import 'flet_store_mixin.dart';
@@ -27,23 +28,32 @@ class FilePickerResultEvent {
2728
}
2829

2930
class FilePickerFile {
31+
final int id;
3032
final String name;
3133
final String? path;
3234
final int size;
3335

34-
FilePickerFile({required this.name, required this.path, required this.size});
36+
FilePickerFile(
37+
{required this.id,
38+
required this.name,
39+
required this.path,
40+
required this.size});
3541

3642
Map<String, dynamic> toJson() =>
37-
<String, dynamic>{'name': name, 'path': path, 'size': size};
43+
<String, dynamic>{'id': id, 'name': name, 'path': path, 'size': size};
3844
}
3945

4046
class FilePickerUploadFile {
47+
final int id;
4148
final String name;
4249
final String uploadUrl;
4350
final String method;
4451

4552
FilePickerUploadFile(
46-
{required this.name, required this.uploadUrl, required this.method});
53+
{required this.id,
54+
required this.name,
55+
required this.uploadUrl,
56+
required this.method});
4757
}
4858

4959
class FilePickerUploadProgressEvent {
@@ -119,17 +129,23 @@ class _FilePickerControlState extends State<FilePickerControl>
119129
!isDesktopPlatform()) {
120130
resetDialogState();
121131
}
132+
122133
widget.backend.triggerControlEvent(
123-
widget.control.id,
124-
"result",
125-
json.encode(FilePickerResultEvent(
126-
path: _path,
127-
files: _files
128-
?.map((f) => FilePickerFile(
129-
name: f.name,
130-
path: kIsWeb ? null : f.path,
131-
size: f.size))
132-
.toList())));
134+
widget.control.id,
135+
"result",
136+
json.encode(FilePickerResultEvent(
137+
path: _path,
138+
files: _files?.asMap().entries.map((entry) {
139+
PlatformFile f = entry.value;
140+
return FilePickerFile(
141+
id: entry.key, // use entry's index as id
142+
name: f.name,
143+
path: kIsWeb ? null : f.path,
144+
size: f.size,
145+
);
146+
}).toList(),
147+
)),
148+
);
133149
}
134150

135151
if (_state != state) {
@@ -154,7 +170,7 @@ class _FilePickerControlState extends State<FilePickerControl>
154170
allowMultiple: allowMultiple,
155171
withData: false,
156172
withReadStream: true)
157-
.then((result) {
173+
.then((FilePickerResult? result) {
158174
debugPrint("pickFiles() completed");
159175
_files = result?.files;
160176
sendEvent();
@@ -177,7 +193,7 @@ class _FilePickerControlState extends State<FilePickerControl>
177193
sendEvent();
178194
});
179195
}
180-
// saveFile
196+
// getDirectoryPath
181197
else if (state?.toLowerCase() == "getdirectorypath" && !kIsWeb) {
182198
FilePicker.platform
183199
.getDirectoryPath(
@@ -207,17 +223,28 @@ class _FilePickerControlState extends State<FilePickerControl>
207223
Future uploadFiles(String filesJson, FletServer server, Uri pageUri) async {
208224
var uj = json.decode(filesJson);
209225
var uploadFiles = (uj as List).map((u) => FilePickerUploadFile(
210-
name: u["name"], uploadUrl: u["upload_url"], method: u["method"]));
226+
id: parseInt(u["id"], -1)!, // -1 = invalid
227+
name: u["name"],
228+
uploadUrl: u["upload_url"],
229+
method: u["method"]));
230+
211231
for (var uf in uploadFiles) {
212-
var file = _files!.firstWhereOrNull((f) => f.name == uf.name);
232+
var file = ((uf.id >= 0 && uf.id < _files!.length)
233+
? _files![uf.id]
234+
: null) // by id
235+
??
236+
_files!.firstWhereOrNull((f) => f.name == uf.name); // by name
237+
213238
if (file != null) {
214239
try {
215240
await uploadFile(
216241
file, server, getFullUploadUrl(pageUri, uf.uploadUrl), uf.method);
217-
_files!.remove(file);
242+
_files!.remove(file); // Remove the uploaded file
218243
} catch (e) {
219244
sendProgress(server, file.name, null, e.toString());
220245
}
246+
} else {
247+
debugPrint("Error: File '${uf.name}' (id: ${uf.id}) not found.");
221248
}
222249
}
223250
}

sdk/python/packages/flet/src/flet/core/file_picker.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class FilePickerFileType(Enum):
3434
class FilePickerUploadFile:
3535
name: str
3636
upload_url: str
37+
id: int = None
3738
method: str = field(default="PUT")
3839

3940

@@ -42,6 +43,7 @@ class FilePickerFile:
4243
name: str
4344
path: str
4445
size: int
46+
id: int
4547

4648

4749
class FilePickerResultEvent(ControlEvent):

0 commit comments

Comments
 (0)