Expand description
Reading and writing what the user chose in a file dialog, the same way on every platform.
FileDialogResult::files turns a choice into PickedFiles. What one holds depends on
where the application runs, and the application does not have to know:
- On a desktop or a phone it is a path.
PickedFile::read,PickedFile::write,PickedFile::entriesandPickedFile::childrun on a Host worker withstd::fs, so the UI thread never waits for the disk. A write goes to a temporary file beside the target and is renamed over it, so a reader never sees half of it. - In a browser there is no path. The Renderer keeps the browser’s handle for the file and
the Host keeps its number. The dialog’s answer carries one reference per file in
Chosen.paths, writtenwebfile:<handle>/<name>, andfiles()turns each into aPickedFile. Every operation then crosses to the Renderer as oneFileCallrecord and comes back as one event: anIoAnswerwith the bytes or the reason, or aFileHandleswith the files a folder holds. When the lastPickedFilefor a handle is dropped, the Host tells the Renderer to let go of it withReleaseFileHandle.
FileDialog::open_file().filter("Text", &["txt", "md"]).show(move |result| {
if let Some(file) = result.files().into_iter().next() {
scope.launch(async move {
if let Ok(bytes) = file.read().await {
document.set(String::from_utf8_lossy(&bytes).into_owned());
}
});
}
});How a file reached the application is PickedFile::delivery. Only a
Delivery::Handle can be written again in place: a browser without the File System
Access API hands over a copy to read (Delivery::Upload) or turns a save into a download
(Delivery::Download), and an application treats its next save as “save as” when
PickedFile::can_write_again is false.
The operations are futures to await on the UI thread, from an effect, a coroutine scope or
a handler. They are not Send on the web, where the answer comes back on the UI thread
inside a Host call.
Structs§
- Picked
File - One file or folder the user chose, to read and write.
Enums§
- Delivery
- How a file reached the application.
- File
Error - Why an operation on a
PickedFiledid not happen.