12

I am using webkitdirectory to select a folder. I want to get the folder name which is selected for uploading.

1 Answer 1

28

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>
Sign up to request clarification or add additional context in comments.

5 Comments

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?
@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.
@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.
@MKR You are right. In new update of google chrome, it is not selecting a folder if it is empty.
what happen if directory is empty and need the path???

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.