Search code examples
gwtbrowsergxt

Browser drop files event GWT


I have a problem with the browser behaviour to drop files. I have a "ContentPanel" which contains a file drag and drop area. when I drag and drop a file there everything is ok but when I drop the file in some other place inside of the "ContentPanel", the browser opens the file (behaviour by default).

How can I avoid this? Nothing should happens in this last case.


Solution

  • It is necessary 2 drop handlers, one for the content panel and other for drag and drop area.

    public FileUploadWidget() {
    
        RootPanel rootPanel = RootPanel.get();
        DropHandler dropHandlerRoot = new DropHandler(rootPanel);
    
        dropHandlerRoot.addFileEventHandler(new FileEventHandler() {
            @Override
            public void onFiles(FileEvent event) {
                // Nothing to do, avoid the default browser 
                // behaviour which is to open the file
            }
        });
    
        // Drag and drop area handler
        dropHandler = new DropHandler(dragAndDropArea);
        dropHandler.addFileEventHandler(new FileEventHandler() {
            @Override
            public void onFiles(FileEvent event) {
                JsArray<File> files = event.getFiles();
    
                for (int i = 0; i < files.length(); ++i) {
                    File file = files.get(i);
                    addFile(file);
                    handleSizeChange();
                }
            }
        });
    }