[英]Vaadin 14 Upload - how to catch event when user clicks Cancel button in choose file dialog
I've tried all existing listeners, but no one can catch this type of event.我已经尝试了所有现有的侦听器,但没有人能够捕捉到这种类型的事件。
Button uploadButton = new Button("Choose a file");
uploadButton.setDisableOnClick(true);
Upload upload = new Upload();
upload.setUploadButton(uploadButton);
User clicks uploadButton, button now disabled.用户点击上传按钮,按钮现在被禁用。 Then in system choose file dialog user clicks Cancel button instead of choosing a file.然后在系统选择文件对话框中,用户单击取消按钮而不是选择文件。 Dialog is closed, no event is fired, uploadButton still disabled.对话框已关闭,未触发任何事件,uploadButton 仍处于禁用状态。 I want to catch event when Cancel button is pressed and enable uploadButton.我想在按下取消按钮时捕获事件并启用上传按钮。
I can confirm the behavior you saw: No events seem to fire when user cancels the file-picker dialog.我可以确认您看到的行为:当用户取消文件选择器对话框时,似乎没有事件触发。
Here is an example app in Vaadin 14.1.21.这是 Vaadin 14.1.21 中的示例应用程序。 I added listeners for several of the event types.我为几种事件类型添加了侦听器。
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 );
}
}
I consider this behavior a feature.我认为这种行为是一个特征。 If the user clicks the button to choose files, but cancels before completing that choice, nothing has really happened.如果用户单击按钮选择文件,但在完成该选择之前取消,则实际上什么也没有发生。 No upload has been attempted.未尝试上传。 So no event should fire as nothing eventful has transpired.因此,不应触发任何事件,因为没有发生任何事件。
Perhaps you should edit your Question to explain your interest in detecting the user having changed their mind about choosing files to upload.也许你应该编辑你的问题来解释你有兴趣检测用户改变了他们选择要上传的文件的想法。 Perhaps there is a better solution to your ultimate goal.也许有更好的解决方案来实现您的最终目标。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.