276

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?

Yogesh Pingle
  • 3,085
  • 2
  • 14
  • 15
  • http://stackoverflow.com/questions/9463368/getting-file-full-path-when-uploading-file-in-html-in-firefox – Nauphal Mar 04 '13 at 12:05
  • [See this for details](http://stackoverflow.com/questions/3489133/full-path-from-file-input-using-jquery#answer-3489167) – palaѕн Mar 04 '13 at 12:07
  • @nauphal thanks for comment but is there any other way to get full path of selected file? – Yogesh Pingle Mar 04 '13 at 12:08
  • 1
    If you're in the case where you want to get the path of a file on the _server_, (for instance building a web interface to a commandline utility to be run on the server) you can always build the relative path, send it over as – dardenfall Oct 24 '13 at 14:27
  • 2
    Possible duplicate of [how to get full path from fileupload using Javascript](https://stackoverflow.com/questions/1635329/how-to-get-full-path-from-fileupload-using-javascript) –  May 10 '18 at 02:19

12 Answers12

186

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.

Syscall
  • 16,959
  • 9
  • 22
  • 41
undefined
  • 136,817
  • 15
  • 158
  • 186
  • 1
    See [this link](http://stackoverflow.com/questions/4459379/preview-an-image-before-it-is-uploaded) as it helped me with the same issue. – Amir Tugi Oct 20 '16 at 18:44
  • ... The title of the linked question is: [Preview an image before it is uploaded](http://stackoverflow.com/questions/4459379/preview-an-image-before-it-is-uploaded). – undefined Oct 20 '16 at 18:49
  • And yet it got me the URL for the path to send to the server – Amir Tugi Oct 20 '16 at 19:08
  • 2
    @AmirTugi That solution reads a file. It has nothing to do with the file's path on user's file system. – undefined Oct 20 '16 at 19:25
  • So how this website could do it? – SaidbakR Nov 15 '17 at 18:26
  • $('input[type="file"]').change(function (e) { var fileName = e.target.files[0].name; alert('The file "' + fileName + '" has been selected.'); console.log(e.target.value); }); – beck kuo Sep 19 '19 at 03:18
  • Is there any update on this (if it's doable now)? Is there any (hacky) workaround to get file's absolute path? – Darren Christopher Nov 15 '19 at 04:51
  • @DarrenChristopher Why do you need the absolute path? – undefined Nov 16 '19 at 16:56
  • @undefined I have a server script running on local machine which will only be used in client from the same machine as for now, so it might be silly to do file transfer from and to the same machine. Plus, the data could be huge, which file transfer over the network is not really feasible. – Darren Christopher Nov 17 '19 at 22:09
109

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):-

JSFIDDLE

Here is the code :-

HTML:-

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

JS:-

$('#i_file').change( function(event) {
    var tmppath = URL.createObjectURL(event.target.files[0]);
    $("img").fadeIn("fast").attr('src',URL.createObjectURL(event.target.files[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.

Syscall
  • 16,959
  • 9
  • 22
  • 41
DWX
  • 2,082
  • 1
  • 11
  • 15
  • Is there a way to do this with other file types: pdf, docx, etc. and instead of the image so show an Icon? For my uses I want to store the files on the page before I send them giving the user a chance to add a comment to up load with it like a message with a text. – user1040975 Apr 03 '15 at 18:21
  • Do I only need to put the content of "tmppath" in the browser address bar? I tried and it doesn't work. – MacGruber Oct 14 '15 at 14:02
  • @GangsarSwaPurba Unfortunately, it doesn't work in IE9 - [see URL.createObjectURL()](https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL#Browser_compatibility). – naXa Feb 08 '16 at 12:23
  • @naXa good, i will edit my comment above. uw, unfurtunately i can't edit my comment – Jamaluddin Rumi Feb 18 '16 at 08:21
  • 2
    In the 3rd line you should use tmppath variable instead of URL.createObjectURL(event.target.files[0]), this's more optimized. – Wessam El Mahdy Oct 08 '17 at 03:42
  • From MDN: "Each time you call createObjectURL(), a new object URL is created, even if you've already created one for the same object. Each of these must be released by calling URL.revokeObjectURL() when you no longer need them.". So use it once and revoke when no longer required. – Kesarion Feb 11 '19 at 15:49
19

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 object.

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">

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

</script>
Dipesh Parmar
  • 25,931
  • 6
  • 53
  • 85
18

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.

Rajshekar Reddy
  • 17,202
  • 3
  • 34
  • 54
14

Did you mean this?

$('#i_file').change( function(event) {
var tmppath = URL.createObjectURL(event.target.files[0]);
    $("img").fadeIn("fast").attr('src',tmppath);       
});
doğukan
  • 14,001
  • 6
  • 27
  • 47
Aytac Gul
  • 189
  • 1
  • 2
10

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);
</script>
Steffen Brem
  • 1,662
  • 17
  • 26
8

One interesting note: although this isn't available in on the web, if you're using JS in Electron then you can do this.

Using the standard HTML5 file input, you'll receive an extra path property on selected files, containing the real file path.

Full docs here: https://github.com/electron/electron/blob/master/docs/api/file-object.md

Tim Perry
  • 8,453
  • 41
  • 62
2

You can, if uploading an entire folder is an option for you

<input type="file" webkitdirectory directory multiple/>

change event will contain:

.target.files[...].webkitRelativePath: "FOLDER/FILE.ext"

but it doesn't contain the whole absolute path, only the relative one. Supported in Firefox also.

phil294
  • 8,501
  • 7
  • 55
  • 84
1

you should never do so... and I think trying it in latest browsers is useless(from what I know)... all latest browsers on the other hand, will not allow this...

some other links that you can go through, to find a workaround like getting the value serverside, but not in clientside(javascript)

Full path from file input using jQuery
How to get the file path from HTML input form in Firefox 3

cristid9
  • 924
  • 14
  • 23
bipen
  • 34,730
  • 9
  • 44
  • 61
0

file element has and array call files it contain all necessary stuff you need

var file = document.getElementById("upload");

file.addEventListener("change", function() {
    for (var i = 0; i < file.files.length; i++) {
        console.log(file.files[i].name);
    }
}, false);
Jeeva Kumar
  • 329
  • 2
  • 8
0

One could use the FileReader API for changing the src of an img element.

https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL

-3

You can get the full path of the selected file to upload only by IE11 and MS Edge.

var fullPath = Request.Form.Files["myFile"].FileName;
Majid Shahabfar
  • 693
  • 1
  • 11
  • 19