summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoradam <adam@vaadin.com>2016-06-30 12:48:06 +0300
committerVaadin Code Review <review@vaadin.com>2016-07-13 12:56:38 +0000
commitce746e0a86f4b110d7c821cdc2542a9677b17367 (patch)
tree860acda034711ace08f343bdeb4f29632a651d17
parent1abf6dcac263ace630698da028308ea0ec5b8636 (diff)
downloadvaadin-framework-ce746e0a86f4b110d7c821cdc2542a9677b17367.tar.gz
vaadin-framework-ce746e0a86f4b110d7c821cdc2542a9677b17367.zip
Fixing drag and drop file upload issue on windows (#19809)
Folder upload is not supported by most of the browsers and can cause StreamVariable methods not fire on some configurations. This fix tries to detect and prevent uploading of folders. Change-Id: Ib6357ddaeda5549065ab8ef5f682f12ad5968bce
-rw-r--r--client/src/main/java/com/vaadin/client/ui/VDragAndDropWrapper.java25
-rw-r--r--client/src/main/java/com/vaadin/client/ui/dd/VHtml5DragEvent.java26
2 files changed, 41 insertions, 10 deletions
diff --git a/client/src/main/java/com/vaadin/client/ui/VDragAndDropWrapper.java b/client/src/main/java/com/vaadin/client/ui/VDragAndDropWrapper.java
index 15ab9a6ce0..4ecdec8b6f 100644
--- a/client/src/main/java/com/vaadin/client/ui/VDragAndDropWrapper.java
+++ b/client/src/main/java/com/vaadin/client/ui/VDragAndDropWrapper.java
@@ -409,21 +409,26 @@ public class VDragAndDropWrapper extends VCustomComponent implements
}
}
- int fileCount = event.getFileCount();
- if (fileCount > 0) {
- transferable.setData("filecount", fileCount);
- for (int i = 0; i < fileCount; i++) {
+ final int eventFileCount = event.getFileCount();
+ int fileIndex = 0;
+ for (int i = 0; i < eventFileCount; i++) {
+ // Transfer only files and not folders
+ if (event.isFile(i)) {
final int fileId = filecounter++;
final VHtml5File file = event.getFile(i);
VConsole.log("Preparing to upload file " + file.getName()
- + " with id " + fileId);
- transferable.setData("fi" + i, "" + fileId);
- transferable.setData("fn" + i, file.getName());
- transferable.setData("ft" + i, file.getType());
- transferable.setData("fs" + i, file.getSize());
+ + " with id " + fileId + ", size="
+ + file.getSize());
+ transferable.setData("fi" + fileIndex, "" + fileId);
+ transferable.setData("fn" + fileIndex, file.getName());
+ transferable.setData("ft" + fileIndex, file.getType());
+ transferable.setData("fs" + fileIndex, file.getSize());
queueFilePost(fileId, file);
+ fileIndex++;
}
-
+ }
+ if (fileIndex > 0) {
+ transferable.setData("filecount", fileIndex);
}
VDragAndDropManager.get().endDrag();
diff --git a/client/src/main/java/com/vaadin/client/ui/dd/VHtml5DragEvent.java b/client/src/main/java/com/vaadin/client/ui/dd/VHtml5DragEvent.java
index 68987e565f..672eac9e06 100644
--- a/client/src/main/java/com/vaadin/client/ui/dd/VHtml5DragEvent.java
+++ b/client/src/main/java/com/vaadin/client/ui/dd/VHtml5DragEvent.java
@@ -80,6 +80,32 @@ public class VHtml5DragEvent extends NativeEvent {
return this.dataTransfer.files[fileIndex];
}-*/;
+ /**
+ * Detects if dropped element is a file. <br>
+ * Always returns <code>true</code> on Safari even if the dropped element is
+ * a folder.
+ */
+ public final native boolean isFile(int fileIndex)
+ /*-{
+ // Chrome >= v21 and Opera >= v?
+ if (this.dataTransfer.items) {
+ var item = this.dataTransfer.items[fileIndex];
+ if (item.webkitGetAsEntry) {
+ return item.webkitGetAsEntry().isFile;
+ }
+ }
+
+ // Zero sized files without a type are also likely to be folders
+ var file = this.dataTransfer.files[fileIndex];
+ if (file.size == 0 && !file.type) {
+ return false;
+ }
+
+ // TODO Make it detect folders on all browsers
+
+ return true;
+ }-*/;
+
public final native void setHtml5DataFlavor(String flavor, String data)
/*-{
this.dataTransfer.setData(flavor, data);