Skip to content
Advertisement

How to get full path of selected file on change of using javascript, jquery-ajax?

How to get full path of file while selecting file using <input type=‘file’>

<input type="file" id="fileUpload">
<script type="text/javascript">
function getFilePath(){
     $('input[type=file]').change(function () {
         var filePath=$('#fileUpload').val(); 
     });
}
</script>

but the filePath var contains only name of selected file, not the full path.
I searched it on net, but it seems that for security reasons browsers (FF,chrome) just give name of file.
Is there any other way to get full path of selected file?

Advertisement

Answer

For security reasons browsers do not allow this, i.e. JavaScript in browser has no access to the File System, however using HTML5 File API, only Firefox provides a mozFullPath property, but if you try to get the value it returns an empty string:

$('input[type=file]').change(function () {
    console.log(this.files[0].mozFullPath);
});

https://jsfiddle.net/SCK5A/

So don’t waste your time.

edit: If you need the file’s path for reading a file you can use the FileReader API instead. Here is a related question on SO: Preview an image before it is uploaded.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement