Select & Drop Files and/or Folders to be parsed

前端 未结 2 1633
无人共我
无人共我 2020-12-18 16:59

I\'ve used a part from a post here on SO ( I can\'t remember it though ) to parse files and folders on Chrome, but I cannot get it to work on Firefox (and to be honest I hav

2条回答
  •  执念已碎
    2020-12-18 17:27

    Nightly 45+ supports directory upload. See Does Firefox support folder upload?

    window.onload = function() {
      document.querySelector("input").onchange = function(e) {
    
        var uploadFile = function(file, path) {
          // handle file uploading
          console.log(file, path)
        };
    
        var iterateFilesAndDirs = function(filesAndDirs, path) {
          for (var i = 0; i < filesAndDirs.length; i++) {
            if (typeof filesAndDirs[i].getFilesAndDirectories === "function") {
              var path = filesAndDirs[i].path;
    
              // this recursion enables deep traversal of directories
              filesAndDirs[i].getFilesAndDirectories().then(function(subFilesAndDirs) {
                // iterate through files and directories in sub-directory
                iterateFilesAndDirs(subFilesAndDirs, path);
              });
            } else {
              uploadFile(filesAndDirs[i], path);
            }
          }
        };
        if ("getFilesAndDirectories" in e.target) {
          e.target.getFilesAndDirectories()
            .then(function(filesAndDirs) {
              iterateFilesAndDirs(filesAndDirs, "/");
            })
        } else {
          // do webkit stuff
        }
      }
    }

    plnkr http://plnkr.co/edit/DSUeZiW4JjvxmRrFnqN0?p=preview

提交回复
热议问题