Hướng dẫn javascript directory path

I am trying to get the directory location of a file, and I'm not sure how to get it. I can't seem to find a module that allows me to do this.

So for example say I have this string:

/this/is/a/path/to/a/file.html

how can I get this:

/this/is/a/path/to/a

I know I can use something like this:

path.substr(0, path.lastIndexOf("/") - 1);

But I am not sure if that is as good of a method as something that might be built in to node.

I have also tried:

var info = url.parse(full_path);
console.log(info);

and the result doesn't return what I am looking for, that gets the full path including the filename.

So, is there something built into node that can do this and do it well?

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?

asked Mar 4, 2013 at 11:59

Yogesh PingleYogesh Pingle

3,3953 gold badges15 silver badges16 bronze badges

5

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

18.3k10 gold badges32 silver badges49 bronze badges

answered Mar 4, 2013 at 12:09

RamRam

141k16 gold badges164 silver badges192 bronze badges

9

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

 



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) --> ["+tmppath+"]");
});

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

Syscall

18.3k10 gold badges32 silver badges49 bronze badges

answered Jul 18, 2014 at 6:07

DWXDWX

2,2121 gold badge13 silver badges15 bronze badges

6

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,


answered Mar 4, 2013 at 12:05

Dipesh ParmarDipesh Parmar

26.8k7 gold badges59 silver badges87 bronze badges

3

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.

answered Nov 22, 2013 at 5:47

Rajshekar ReddyRajshekar Reddy

18.2k3 gold badges37 silver badges59 bronze badges

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


answered Mar 30, 2016 at 14:26

Steffen BremSteffen Brem

1,72018 silver badges28 bronze badges

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

18.5k10 gold badges42 silver badges60 bronze badges

answered Nov 23, 2014 at 19:10

Aytac GulAytac Gul

1851 silver badge2 bronze badges

1

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

answered Nov 18, 2019 at 14:23

Tim PerryTim Perry

10.5k1 gold badge50 silver badges78 bronze badges

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


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.

answered Mar 4, 2017 at 9:31

phil294phil294

9,3978 gold badges61 silver badges91 bronze badges

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

answered Dec 13, 2017 at 7:56

Jeeva KumarJeeva Kumar

3332 silver badges8 bronze badges

1

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

This is the correct form.

answered Aug 1, 2021 at 13:57

Alexie01Alexie01

731 silver badge7 bronze badges

2

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;

answered Apr 1, 2019 at 11:03

Majid ShahabfarMajid Shahabfar

2,8291 gold badge25 silver badges34 bronze badges

Not the answer you're looking for? Browse other questions tagged javascript jquery file-upload filepath or ask your own question.