gwt - how can i trigger clickEvent on FileUpload widget?

后端 未结 3 577
小蘑菇
小蘑菇 2021-01-14 03:25

I\' m trying to create a simple Button and when it\'s clicked I want to trigger a hidden FileUpload widget which is inside a FormPanel. What I have done until now is two thi

相关标签:
3条回答
  • 2021-01-14 03:38
    myFileUpload.getElement().<InputElement>cast().click()
    

    is what you're looking for.

    For it to work in WebKit-based browsers (Chrome, Safari), the FileUpload has to be "moved out of view" but not hidden (as in setVisible(false), which sets the CSS display property to none), i.e. something like (in CSS): position:absolute; top: -1000px; left: -1000px;

    0 讨论(0)
  • 2021-01-14 03:38

    I believe Thomas Broyer's answer is out of date now. I have just successfully used myFileUpload.click() with no problems.

    I also used setVisible(false) on the the FileUpload field and it works in Chrome. Haven't tested in Safari but it would seem those issues have been resolved now.

    0 讨论(0)
  • 2021-01-14 03:48

    You can also use JSNI like this:

    private native void triggerClick(Element e) /*-{
        e.click();
    }-*/;
    

    Then call

    triggerClick(fileInput.getElement());
    
    0 讨论(0)
提交回复
热议问题