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

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

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?


Solution 1:

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 () {

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.

Solution 2:

Try This:

It’ll give you a temporary path not the accurate path, you can use this script if you want to show selected images as in this jsfiddle example(Try it by selectng images as well as other files):-


Here is the code :-


<input type="file" id="i_file" value=""> 
<input type="button" id="i_submit" value="Submit">
<img src="" width="200" style="display:none;" />
<div id="disp_tmp_path"></div>


$('#i_file').change( function(event) {
    var tmppath = URL.createObjectURL([0]);

    $("#disp_tmp_path").html("Temporary Path(Copy it and try pasting it in browser address bar) --> <strong>["+tmppath+"]</strong>");

Its not exactly what you were looking for, but may be it can help you somewhere.

Solution 3:

You cannot do so – the browser will not allow this because of security concerns.

When a file is selected by using the input type=file object, the value
of the value property depends on the value of the “Include local
directory path when uploading files to a server” security setting for
the security zone used to display the Web page containing the input

The fully qualified filename of the selected file is returned only
when this setting is enabled. When the setting is disabled, Internet
Explorer 8 replaces the local drive and directory path with the string
C:\fakepath\ in order to prevent inappropriate information disclosure.

And other

You missed ); this at the end of the change event function.

Also do not create function for change event instead just use it as below,

<script type="text/javascript">

        $('#fileUpload').on('change',function ()
            var filePath = $(this).val();


Solution 4:

You can’t.
Security stops you for knowing anything about the filing system of the client computer – it may not even have one! It could be a MAC, a PC, a Tablet or an internet enabled fridge – you don’t know, can’t know and won’t know. And letting you have the full path could give you some information about the client – particularly if it is a network drive for example.

In fact you can get it under particular conditions, but it requires an ActiveX control, and will not work in 99.99% of circumstances.

You can’t use it to restore the file to the original location anyway (as you have absolutely no control over where downloads are stored, or even if they are stored) so in practice it is not a lot of use to you anyway.

Solution 5:

Did you mean this?

$('#i_file').change( function(event) {
var tmppath = URL.createObjectURL([0]);

Solution 6:

You can use the following code to get a working local URL for the uploaded file:

<script type="text/javascript">    
    var path = (window.URL || window.webkitURL).createObjectURL(file);
    console.log('path', path);