Skip to content

Instantly share code, notes, and snippets.

@ibaca
Created July 15, 2017 07:33
Show Gist options
  • Save ibaca/a8a84b6e7b63259109fd782d7dbadd8d to your computer and use it in GitHub Desktop.
Save ibaca/a8a84b6e7b63259109fd782d7dbadd8d to your computer and use it in GitHub Desktop.
GWT Elemento file drop onto a browser div and then upload the file example.
package dnd;
import static elemental2.dom.DomGlobal.document;
import static org.jboss.gwt.elemento.core.EventType.dragend;
import static org.jboss.gwt.elemento.core.EventType.dragover;
import static org.jboss.gwt.elemento.core.EventType.drop;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import elemental2.dom.DataTransfer;
import elemental2.dom.File;
import elemental2.dom.FileList;
import elemental2.dom.FormData;
import elemental2.dom.XMLHttpRequest;
import org.jboss.gwt.elemento.core.Elements;
/**
* https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API/File_drag_and_drop
*
* https://github.com/hal/elemento
*
* <inherits name="org.jboss.gwt.elemento.Core"/>
*/
public class ElementoFileDndUploadExample implements EntryPoint {
@Override public void onModuleLoad() {
document.body.appendChild(Elements.div()
.style("border: 3px dashed grey; width: 300px; height: 300px;")
.textContent("Drop files here!")
.on(dragover, evt -> {
GWT.log("dragOver");
evt.stopPropagation();
evt.preventDefault();
DataTransfer dataTransfer = evt.dataTransfer;
dataTransfer.dropEffect = "copy";
})
.on(drop, evt -> {
GWT.log("drop");
evt.stopPropagation();
evt.preventDefault();
DataTransfer dataTransfer = evt.dataTransfer;
FileList files = dataTransfer.files;
for (int i = 0; i < files.length; i++) {
File file = files.item(i);
GWT.log("file: " + file.name);
FormData formData = new FormData();
formData.append("file", file);
XMLHttpRequest xmlHttpRequest = new XMLHttpRequest();
xmlHttpRequest.open("POST", "/");
xmlHttpRequest.send(formData);
}
})
.on(dragend, evt -> GWT.log("dragEnd"))
.asElement());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment