Vaadin 14 上传 - 当用户在选择文件对话框中单击取消按钮时如何捕获事件

Vaadin 14 Upload - how to catch event when user clicks Cancel button in choose file dialog

我已经尝试了所有现有的侦听器,但没有人能捕捉到此类事件。

Button uploadButton = new Button("Choose a file");
uploadButton.setDisableOnClick(true);
Upload upload = new Upload();
upload.setUploadButton(uploadButton);

用户点击上传按钮,按钮现在被禁用。然后在系统选择文件对话框中,用户单击取消按钮而不是选择文件。对话框已关闭,未触发任何事件,上传按钮仍处于禁用状态。 我想在按下取消按钮时捕获事件并启用上传按钮。

行为已确认

我可以确认您看到的行为:当用户取消文件选择器对话框时似乎没有事件触发。

这是 Vaadin 14.1.21 中的示例应用程序。我为几种事件类型添加了监听器。

package work.basil.example;

import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.dependency.CssImport;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.upload.*;
import com.vaadin.flow.component.upload.receivers.MemoryBuffer;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.server.PWA;

import java.time.Instant;

/**
 * The main view contains a button and a click listener.
 */
@Route ( "" )
@PWA ( name = "Project Base for Vaadin", shortName = "Project Base" )
@CssImport ( "./styles/shared-styles.css" )
@CssImport ( value = "./styles/vaadin-text-field-styles.css", themeFor = "vaadin-text-field" )
public class MainView extends VerticalLayout
{

    public MainView ( )
    {
        MemoryBuffer buffer = new MemoryBuffer();
        Upload upload = new Upload( buffer );

        upload.addStartedListener( ( StartedEvent event ) -> System.out.println( event.getClass().getCanonicalName() + " happened " + Instant.now() ) );

        upload.addSucceededListener( ( SucceededEvent event ) -> System.out.println( event.getClass().getCanonicalName() + " happened " + Instant.now() ) );

        upload.addFinishedListener( ( FinishedEvent event ) -> System.out.println( event.getClass().getCanonicalName() + " happened " + Instant.now() ) );

        upload.addAllFinishedListener( ( AllFinishedEvent event ) -> System.out.println( event.getClass().getCanonicalName() + " happened " + Instant.now() ) );

        upload.addProgressListener( ( ProgressUpdateEvent event ) -> System.out.println( event.getClass().getCanonicalName() + " happened " + Instant.now() ) );

        upload.addFileRejectedListener( ( FileRejectedEvent event ) -> System.out.println( event.getClass().getCanonicalName() + " happened " + Instant.now() ) );

        upload.addFailedListener( ( FailedEvent event ) -> System.out.println( event.getClass().getCanonicalName() + " happened " + Instant.now() ) );

        upload.addFileRejectedListener( ( FileRejectedEvent event ) -> System.out.println( event.getClass().getCanonicalName() + " happened " + Instant.now() ) );


        this.add( upload );
    }
}

功能,不是错误

我认为这种行为是一种特征。如果用户单击按钮选择文件,但在完成该选择之前取消,则什么都没有发生。未尝试上传。所以没有事件应该触发,因为没有发生任何事件。

也许您应该编辑您的问题,以解释您有兴趣检测用户改变了选择上传文件的想法。也许对您的最终目标有更好的解决方案。