I am using webkitdirectory to select a folder. I want to get the folder name which is selected for uploading.
1 Answer
html:
<input type="file" id="FileUpload" onchange="selectFolder(event)" webkitdirectory mozdirectory msdirectory odirectory directory multiple />
javascript:
<script type="text/javascript">
function selectFolder(e) {
var theFiles = e.target.files;
var relativePath = theFiles[0].webkitRelativePath;
var folder = relativePath.split("/");
alert(folder[0]);
}
</script>
-
2I have noticed one limitation in above solution. If you have a
blank
folder then it doesn't work. Any idea how to fix it?– MKRApr 1, 2019 at 7:52 -
@MKR what do you mean by blank folder? I'm not familiar with Mac or Linux, but I can't rename a folder to just spaces, or an empty string, which is what I take "blank" to mean. Jun 17, 2019 at 14:41
-
3@donutguy640 Suppose I have created a folder "ABC". That "ABC" folder does not contains any other files yet. In that scenario you cannot select the folder name.– MKRJun 18, 2019 at 20:42
-
3@MKR You are right. In new update of google chrome, it is not selecting a folder if it is empty.– NoobishJun 19, 2019 at 14:09
-
2what happen if directory is empty and need the path???– user4717133Aug 22, 2020 at 17:36