Is there any way to get passed empty folders when drag dropping folder structure using dropzone.js?

Viewed 28

I'm using dropzone.js to be able to easily upload files and folders. I do not get an notification for empty folders, is there any way to have an event fired for empty folders?

Thank you.

1 Answers

So this is what I came up with, not the most elegant solution, but does what I need to be able to handle emptyfolders. Would be great if dropzone.js had an option for enabling emptyfolders to be returned.

I made the following changes to the dropzone.js

At the very top I added

var passedFolders = [];
// used to store the names of fodlers that were passed, with either files or without
// the key is the path name, the value is true if empty, false if it had contents

Then under the section

 var readEntries = function readEntries() {
    return dirReader.readEntries(function (entries) {

change it to

var readEntries = function readEntries() {
        return dirReader.readEntries(function (entries) {
          if (entries.length > 0) {
            var _iterator7 = dropzone_createForOfIteratorHelper(entries, true),
                _step7;

            try {
              for (_iterator7.s(); !(_step7 = _iterator7.n()).done;) {
                var entry = _step7.value;

                if (entry.isFile) {
                    //There is a a file in the folder, so we can set it's value to false
                    passedFolders[entry.fullPath.substring(0, entry.fullPath.lastIndexOf(entry.name)-1)] = false //-1 to remove the last slash
                    
                    
                  entry.file(function (file) {
                    if (_this5.options.ignoreHiddenFiles && file.name.substring(0, 1) === ".") {
                      return;
                    }

                    file.fullPath = "".concat(path, "/").concat(file.name);
                    return _this5.addFile(file);
                  });
                } else if (entry.isDirectory) {
                    //This is just a folder, not a file. Store the folder path information set to true, it will remain true if the folder remains empty
                    passedFolders[entry.fullPath] = true; 
                  _this5._addFilesFromDirectory(entry, "".concat(path, "/").concat(entry.name));
                }
              } // Recursively call readEntries() again, since browser only handle
              // the first 100 entries.
              // See: https://developer.mozilla.org/en-US/docs/Web/API/DirectoryReader#readEntries

            } catch (err) {
              _iterator7.e(err);
            } finally {
              _iterator7.f();
            }

            readEntries();
          }

          return null;
        }, errorHandler);
      };

Now passedFolders will contain an associate array, where the key is the pathname and the value is true if it is empty, false if it contained files.

Then under the dropzone init, when the file uploads are complete you can choose what to do with empty folders

this.on('queuecomplete', function (file, json) {
                    alert("Uploads complete, now we can deal with the empty folders");
                    for (var key in passedFolders) {
                        if (passedFolders[key]) {
                        alert(key); 
                        }
                    }
                });
Related