上面的小提琴是我的代码的精简版本,只是为了突出问题(尝试将文件拖放到窗口中)。基本上,webkitGetAsEntry().file()
不会让我写入其范围之外的任何内容,但是,如果您拖动文件然后手动执行console.log(fileList)
(但是,jsfiddle 阻止了此操作),它可以正常工作。任何帮助将不胜感激,谢谢!
上传.js
function Upload() {
_this = this;
this.fileList = 'no file';
this.fire = function(droppedFiles) {
for(i = 0; i< droppedFiles.length; i++) {
this.buildFileSource(droppedFiles[i].webkitGetAsEntry());
}
//This returns the original string
console.log(this.fileList);
}
this.buildFileSource = function(item, path) {
if(item.isFile) {
item.file(function(file) {
_this.fileList = 'file';
//This works as expected
console.log(_this.fileList);
} );
}
};
}
//Event listeners for dragging
$(document).ready(function() {
window.addEventListener("dragenter", function(e) {
event.stopPropagation();
event.preventDefault();
return false;
}, false);
window.addEventListener("dragover", function(e) {
event.stopPropagation();
event.preventDefault();
return false;
}, false);
window.addEventListener("dragleave", function(e) {
event.stopPropagation();
event.preventDefault();
return false;
}, false);
window.addEventListener("drop", function(e) {
e.stopPropagation();
e.preventDefault();
upload = new Upload;
upload.fire(e.dataTransfer.items);
return false;
}, false);
});