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>
5 Comments
MKR
I have noticed one limitation in above solution. If you have a
blank folder then it doesn't work. Any idea how to fix it?donutguy640
@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.
MKR
@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.
Noobish
@MKR You are right. In new update of google chrome, it is not selecting a folder if it is empty.