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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

ndonkoHenri
Copy link
Contributor

@ndonkoHenri ndonkoHenri commented Mar 7, 2025

Resolves #5037

Test Code

import random

import flet as ft


def main(page: ft.Page):
    def pick_files_result(e: ft.FilePickerResultEvent):
        selected_files.value = (
            ", ".join(map(lambda f: f.name, e.files)) if e.files else "Cancelled!"
        )
        selected_files.update()

    def upload_files(e):
        upload_list = []
        if fp.result is not None and fp.result.files is not None:
            for f in fp.result.files:
                new_name = f"{random.randint(1, 1000)}_{f.name}"
                upload_list.append(
                    ft.FilePickerUploadFile(
                        name=new_name,
                        upload_url=page.get_upload_url(new_name, 600),
                        id=f.id,
                    )
                )
            fp.upload(upload_list)

    fp = ft.FilePicker(on_result=pick_files_result)
    page.overlay.append(fp)

    page.add(
        ft.Row(
            [
                ft.ElevatedButton("Upload", on_click=upload_files),
                ft.ElevatedButton(
                    "Pick files",
                    icon=ft.Icons.UPLOAD_FILE,
                    on_click=lambda _: fp.pick_files(allow_multiple=True),
                ),
                selected_files := ft.Text(),
            ]
        )
    )


ft.app(main, upload_dir="uploads")

Summary by Sourcery

New Features:

  • The FilePicker.upload() method now supports specifying a new name for each file in the upload list, allowing modification of filenames before upload.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FilePicker upload fails if original filename is modified
1 participant