Select folder instead of single file - input

久未见 提交于 2021-02-14 18:44:13

问题


I want the input type to be folder and not a single file. How can I select a folder instead of just a single file. Also how can I then access each file in that selected folder. I tried this to select a folder but didn't work. I am on chrome.

 <input id="myInput" type="file" style={{visibility: 'hidden'}} webkitdirectory directory multiple/>


回答1:


You are looking for the files property, which returns a filelist. Use length to get the number of files then use a for statement to do the same for all files, increasing the count by 1 each time

var folder = document.getElementById("myInput");
folder.onchange=function(){
  var files = folder.files,
      len = files.length,
      i;
  for(i=0;i<len;i+=1){
    console.log(files[i]);
  }
}
 <input id="myInput" type="file" webkitdirectory directory multiple>


来源:https://stackoverflow.com/questions/43958335/select-folder-instead-of-single-file-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!