【问题标题】:Vaadin 14 Upload - how to catch event when user clicks Cancel button in choose file dialogVaadin 14 Upload - 当用户在选择文件对话框中单击“取消”按钮时如何捕获事件
【发布时间】:2020-03-30 09:34:09
【问题描述】:

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

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

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

【问题讨论】:

标签: java vaadin vaadin-flow vaadin14


【解决方案1】:

行为确认

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

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

功能,而不是错误

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

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

【讨论】:

  • 感谢您的回答。我同意,如果没有任何反应,则不应在服务器端触发任何事件。但似乎 disableOnClick 按钮属性不能很好地与 Upload 组件一起使用。按钮在客户端点击后被禁用,但无法再次启用。
  • @SapogaN 我建议您根据评论的具体情况发布另一个问题。询问类似“用户取消文件选择器对话框后如何使用上传组件启用 disableOnClick 按钮”。
  • 上传被禁用的事实是一个错误,应该由 vaadin 解决。不存在取消文件选择的事件这一事实不是错误。您应该打开一个关于 disableOnClick 在取消时无法按预期工作的问题。作为一种解决方法,我可以想象您可以在其中一个侦听器中手动禁用上传,也许在 startedListener 中?
  • @kscherrer 如果您运行我的示例代码,您会看到当您单击下载小部件然后取消出现的文件选择器对话框时,这些事件都不会触发。也许还有其他触发事件?
  • 不,我站在你这边,我知道这些事件都不会触发(而且我不认为你可以在Upload 的 java api 中收听更多事件)。我实际上怀疑文件选择器窗口甚至不是 web 组件可以访问的东西,所以永远不会有这样的事件可以听(需要引用)。 OP最好不要使用上传的禁用点击,而它是这样实现的。 vaadin 需要更改 disableOnClick 的功能,因此它将在 startedListener 中禁用,而不是在单击时禁用,只是因为没有 cancel-picker 事件
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-16
  • 1970-01-01
  • 2014-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多