I can't delete the list of uploaded files

Happy new year!!!

With Vaadin 25 Upload, something as simple as

this.uploade.clearFileList();

is not working for me. I know that JacksonUtils is now used internally, but it worked fine in version 24.

Could you describe in more detail how it does not work. I mean if it doesn’t and there is a regression, it should be reported as a bug at GitHub · Where software is built I checked that both the old code and new one sets empty array in order to clear the file list. Furthermore there is set of tests in UploadIT.java to verify this works. The test was not updated during the recent change, which indicates the test still passes.

1 Like

It’s quite unusual and uncommon; it’s surprising that you need access method for this, to be honest.

Vaadin 25.0.2 and Spring Boot 4.0.1

@UIScope
@SpringComponent
public class Uploader extends Upload {

    public Uploader() {
        setMaxFiles(2);
        addFileRejectedListener(event -> {
            String errorMessage = event.getErrorMessage();
            Notification notification = Notification.show(errorMessage, 5000,
                    Notification.Position.MIDDLE);
            notification.addThemeVariants(NotificationVariant.LUMO_ERROR);
        });

        setAcceptedFileTypes(MediaType.TEXT_PLAIN_VALUE, "text/plain");
        setUploadHandler(uploadHandler -> {
            try (var input = uploadHandler.getInputStream();
                 var reader = new BufferedReader(new InputStreamReader(input))) {
                reader.lines().forEach(System.out::println);
                getUI().ifPresent(ui -> ui.access(this::clearFileList));
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }
}
@PageTitle("Hello World")
@Route("")
@Menu(order = 0, icon = LineAwesomeIconUrl.GLOBE_SOLID)
public class HelloWorldView extends HorizontalLayout {

    private TextField name;
    private Button sayHello;

    public HelloWorldView(Uploader uploader) {
        name = new TextField("Your name");
        sayHello = new Button("Say hello");
        sayHello.addClickListener(e -> {
            Notification.show("Hello " + name.getValue() + " and clear files");
            uploader.clearFileList();
        });
        sayHello.addClickShortcut(Key.ENTER);
        setMargin(true);
        setVerticalComponentAlignment(Alignment.END, name, sayHello);

        add(name, sayHello, uploader);

    }

}

Well, So far, this is working well for me.

    @Override
    public void clearFileList() {
        getElement().executeJs("this.files = [];" + "return this.files;");
    }

Ok, now I understand. UploadHandler is not originally purposed to have calls to UI logic, just to process incoming data, nothing else. And indeed if you add UI logic there, you need to use ui.access or provided abstract classes, which have useful callbacks, which are run in ui.access automatically, so you do not have add it, see

So, you probably would like to use:

 whenComplete(success -> {
   if (success) {
      clearFileList();
   }
})