FilePicker service allows you to use the native file explorer to pick single or multiple files, with extension filtering support and upload capabilities.
Usage
Add theFilePicker service to your page overlay and use its methods to open file dialogs:
Methods
pick_files()
Opens a file picker dialog to select one or more files.
The title of the dialog window.
The initial directory where the dialog should open.
The file types allowed to be selected. Options:
ANY, MEDIA, IMAGE, VIDEO, AUDIO, CUSTOM.The allowed file extensions. Only effective when
file_type is FilePickerFileType.CUSTOM.Allow the selection of multiple files at once.
Read selected file contents into the
bytes field of FilePickerFile.FilePickerFile objects representing the selected files.
Example:
save_file()
Opens a save file dialog which lets the user select a file path and name to save a file.
The title of the dialog window.
The default file name. Required in web mode.
The initial directory where the dialog should open.
The file types allowed to be selected.
The allowed file extensions.
The contents of the file. Required in web, iOS, or Android modes.
None if the dialog was cancelled.
Note: On desktop platforms, this method only opens a dialog for the user to select a location and file name, and returns the chosen path. The file itself is not created or saved.
Example:
get_directory_path()
Selects a directory and returns its absolute path.
The title of the dialog window.
The initial directory where the dialog should open.
None if the dialog was cancelled.
Platform Support: Desktop platforms only (not available in web mode).
Raises: FletUnsupportedPlatformException if called in web mode.
Example:
upload()
Uploads picked files to specified upload URLs.
A list of
FilePickerUploadFile objects specifying which files to upload and where.pick_files() must first be called to ensure the internal file picker selection is not empty. Once called, Flet asynchronously starts uploading selected files one-by-one and reports progress via the on_upload event.
Example:
Events
on_upload
Called when a file is uploaded via the upload() method.
FilePickerUploadEvent with:
file_name(str): The name of the uploaded fileprogress(Optional[float]): A value from 0.0 to 1.0 representing upload progresserror(Optional[str]): An error message if the upload failed
- Once with
0.0progress before the upload starts - Once with
1.0progress when the upload completes - For files larger than 1 MB, additional progress events are emitted at every 10% increment
Data Classes
FilePickerFile
Metadata for a file selected via pick_files().
Properties:
id(int): Selection-scoped file identifier assigned by Fletname(str): File name (basename), without directory pathsize(int): File size in bytespath(Optional[str]): Absolute path to the file (None on web)bytes(Optional[bytes]): File contents (only whenwith_data=True)
FilePickerUploadFile
Upload descriptor for one file to be uploaded.
Properties:
upload_url(str): Upload destination URLmethod(str): HTTP method (usuallyPUTorPOST), defaults toPUTid(Optional[int]): File ID fromFilePickerFilename(Optional[str]): File name as fallback when ID is missing
FilePickerFileType
Enum defining allowed file types:
ANY: Allows any file typeMEDIA: A combination ofVIDEOandIMAGEIMAGE: Allows only image filesVIDEO: Allows only video filesAUDIO: Allows only audio filesCUSTOM: Allows only custom file types specified inallowed_extensions
Complete Example
Platform Availability
| Method | Android | iOS | macOS | Windows | Linux | Web |
|---|---|---|---|---|---|---|
pick_files() | ✓ | ✓ | ✓ | ✓ | ✓* | ✓ |
save_file() | ✓ | ✓ | ✓ | ✓ | ✓* | ✓ |
get_directory_path() | ✓ | ✓ | ✓ | ✓ | ✓* | ✗ |
upload() | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |