0

I am following Folder Drag Drop for folder upload.

function onDrop(e) {
  e.preventDefault();
  e.stopPropagation();
  var items = e.dataTransfer.items;
  for (var i=0; i<items.length; i++) {
    var item = items[i].webkitGetAsEntry();
    if (item) {
      traverseFileTree(item, path="", function(){
        //Recursion Complete (Not invoked)
      });
    }
  }
}
var setFlag = true;  
function traverseFileTree(item, path, callback) {
  path = path || "";
  if (item.isFile) {
    item.file(function(file) {

       if(setFlag)callback(null);
    });
  } else if (item.isDirectory) {
    var dirReader = item.createReader();
    dirReader.readEntries(function(entries) {
      for (var i=0; i<entries.length; i++)
        if(entries[i].isDirectory)setFlag = false;

      for (var i=0; i<entries.length; i++) {
        traverseFileTree(entries[i], path + item.name + "/",callback);
      }
    });
  }
}

The above condition check for end of recursion does not work. Since the number of nested files and folders vary, any efficient method to check for end of recursion.

1 Answer 1

0

How are you supposed to do that when your traverseFileTree doesn't have a third parameter, which would be the callback?

function traverseFileTree(item, path, CALLBACK){...}

Also, that function, while not being assigned to parameter, doesn't even get called inside that function. How do you expect it to run?

You need to add some logic to indicate to your function that it has accessed all nodes. Then you call the callback.:

function traverseFileTree(item, path, callback){
  ...
  if(allNodesAccessed) callback.call(null);
  ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yep, ive rephrased the question accordingly. My question is allNodesAccessed logic part.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.