1

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

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

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

4

1 回答 1

0

行为确认

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

这是 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 );
    }
}

功能,而不是错误

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

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

于 2020-03-31T02:37:35.583 回答