0

just a quick question,

mission is to get a folder directory name. example: _C:\ThisFolder\myFolder_.

and I've used html input type="file" but I'm only able to get the file name, plus type="file" works like one want to upload something, while I just want to get the folder path of user selection.

<label> Update to  : </label>
<input type="file" class="form-control" id="dirpath" name="dirpath" directory />

is there any other approach that I could use, please advice, thanks.

s.young
  • 25
  • 2
  • 7
  • I don't think you can do that due to security reasons. – Ropali Munshi Dec 12 '18 at 04:39
  • You can select directory by file input and get folder name. Here is an example https://www.askingbox.com/question/html5-get-folder-name-from-file-input – Alex Nikulin Dec 12 '18 at 05:20
  • This perfectly answers your question [link](https://stackoverflow.com/questions/15201071/how-to-get-full-path-of-selected-file-on-change-of-input-type-file-using-jav) – saur Dec 12 '18 at 05:39
  • New in 2022 - See here: https://stackoverflow.com/a/73713676/1909132 – zvi Sep 14 '22 at 08:22

3 Answers3

2

Due to security reasons browser does not allow it. Browser has no access to the file system. If you need the file's path for reading a file you can use the FileReader API instead.

Praveen Gupta
  • 246
  • 1
  • 5
  • what I need is something like selecting a folder to save file before begin downloading it? can't I do something like that? – s.young Dec 12 '18 at 06:42
0

Try this code,

   
     $('#dirpath').on('change',function ()
            {
                var filePath = $(this).val();
                console.log(filePath);
            });
<label> Update to  : </label>
    <input type="file" class="form-control" id="dirpath" onchange="fileget(event)" name="dirpath" directory />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
Kmg Kumar
  • 587
  • 2
  • 20
0

Maybe you can just use it like this:

<label> Update to  : </label>
<input type="file" class="form-control" id="dirpath" name="dirpath" directory onSubmit = getPath()/>

<script language="javascript" type="text/javascript">
function getPath() {
     var inputName = document.getElementById('dirname');
     var Path;

     Path = inputName.value;
}
</script>