I'm trying to upload folders using the FileSystem API.
dropzone.ondrop = function(e) {
e.preventDefault();
let items=e.dataTransfer.items;
let formdata=new FormData();
function scanFiles(item){
console.log("In scanFiles")
if (item.isFile) {
console.log("Typecasting...");
item.file(function(file) {
console.log(file);
formdata.append('directory',file);
});
}
else if (item.isDirectory) {
console.log("Directory...")
let directoryReader = item.createReader();
directoryReader.readEntries(function(entries) {
entries.forEach(function(entry) {
scanFiles(entry);
});
});
}
}
for (let i=0; i<items.length; i++) {
let item = items[i].webkitGetAsEntry();
console.log(item);
if (item) {
console.log("Calling scan");
scanFiles(item);
console.log("Ending scan");
}
}
var xhr=new XMLHttpRequest();
xhr.open('post','uploadFolder/');
xhr.send(formdata);
}
Basically, what the code does is take a dropped folder, read its contents and if its a file, attaches it as a file input to a form, and send it to the required url for further processing. Okay, so the problem is when I run it, the console output is
FileSystemFileEntry { isFile: true, isDirectory: false, name: "Test.o", fullPath: "/Test.o", filesystem: FileSystem }
Calling scan
In scanFiles
Typecasting...
Ending scan
File { name: "Test.o", lastModified: 1506856693000, lastModifiedDate: Date 2017-10-01T11:18:13.000Z, webkitRelativePath: "", size: 6240, type: "" }
The file object is created after all lines in the script have been executed and so an incorrect request is sent to the url.
I guess this is due to the closure nature of the file() function. Can someone please tell me how I can get the file input in the proper manner?