I used this:

                $('input[type=file]').val()                              

to get the file proper noun selected, merely it returned the full path, every bit in "C:\fakepath\filename.doc". The "fakepath" part was actually there - non sure if it's supposed to be, only this is my get-go time working with the filename of file uploads.

How can I just get the file name (filename.dr.)?

user avatar

manji

46.3k 4 gilt badges xc silver badges 100 bronze badges

asked Jun 16, 2011 at 0:16

user avatar

1

                  var filename = $('input[type=file]').val().split('\\').popular();                                  

or y'all could simply practise (because it's always C:\fakepath that is added for security reasons):

                  var filename = $('input[type=file]').val().supplant(/C:\\fakepath\\/i, '')                                  

answered Jun 16, 2011 at 0:21

user avatar

9

Yous but need to practise the code beneath. The kickoff [0] is to access the HTML chemical element and second [0] is to access the first file of the file upload (I included a validation in instance that at that place is no file):

                                      var filename = $('input[type=file]')[0].files.length ? ('input[type=file]')[0].files[0].name : "";                                  

answered Jun 2, 2014 at nineteen:42

user avatar

2

Become path work with all Os

                  var filename = $('input[type=file]').val().replace(/.*(\/|\\)/, '');                                  

Example

                  C:\fakepath\filename.physician  /var/fakepath/filename.doc                                  

Both render

                  filename.doc filename.doc                                  

answered Jul 17, 2013 at 4:00

user avatar

1

Chrome returns C:\fakepath\... for security reasons - a website should not be able to obtain information well-nigh your computer such as the path to a file on your calculator.

To become just the filename portion of a string, you can utilise split()...

                var file = path.split('\\').pop();                              

jsFiddle.

...or a regular expression...

                var file = path.match(/\\([^\\]+)$/)[1];                              

jsFiddle.

...or lastIndexOf()...

                var file = path.substr(path.lastIndexOf('\\') + 1);                              

jsFiddle.

answered Jun 16, 2011 at 0:21

user avatar

Hither is how I do it, it works pretty well.

In your HTML exercise:

                  <input type="file" name="Att_AttributeID" onchange="fileSelect(upshot)" class="inputField" />                                  

Then in your js file create a uncomplicated function:

                  office fileSelect(id, e){     panel.log(e.target.files[0].name); }                                  

If you lot're doing multiple files, you should also exist able to become the listing by looping over this:

                  east.target.files[0].proper noun                                  

user avatar

Fortin

151 3 silver badges fourteen bronze badges

answered Apr 10, 2013 at 16:39

user avatar

1

possibly some add-on for avoid fakepath:

                var fileName = $('input[type=file]').val(); var clean=fileName.split('\\').pop(); // clean from C:\fakepath OR C:\fake_path  alert('clean file name : '+ fileName);                              

answered November 11, 2011 at iv:05

user avatar

How virtually something similar this?

                var pathArray = $('input[blazon=file]').val().dissever('\\'); alarm(pathArray[pathArray.length - one]);                              

answered Jun 16, 2011 at 0:21

user avatar

Does information technology accept to be jquery? Or can y'all merely use JavaScript'south native yourpath.split("\\") to divide the string to an array?

answered Jun 16, 2011 at 0:21

user avatar

Get the start file from the control and so get the proper name of the file, it volition ignore the file path on Chrome, and also will brand correction of path for IE browsers. On saving the file, you have to utilise System.io.Path.GetFileName method to get the file name only for IE browsers

                  var fileUpload    = $("#ContentPlaceHolder1_FileUpload_mediaFile").go(0);  var files         =  fileUpload.files;  var mediafilename = "";   for (var i = 0; i < files.length; i++) {    mediafilename = files[i].name;  }                                  

user avatar

Paul Roub

35.7k 27 golden badges 78 silver badges 87 bronze badges

answered January 29, 2014 at xix:39

user avatar

0

                <script blazon="text/javascript">      $('#upload').on('change',office(){        // output raw value of file input        $('#filename').html($(this).val().supercede(/.*(\/|\\)/, ''));           // or, manipulate it further with regex etc.         var filename = $(this).val().replace(/.*(\/|\\)/, '');         // .. exercise your magic          $('#filename').html(filename);     }); </script>                              

user avatar

Paul Karam

3,723 vi aureate badges 29 argent badges 46 bronze badges

answered Mar 13, 2018 at 7:55

user avatar

Here y'all can call like this Let this is my Input File control

                                  <input type="file" title="search paradigm" id="file" proper noun="file" onchange="show(this)"  />                              

Now here is my Jquery which go called in one case you select the file

                <script type="text/javascript">     function bear witness(input) {         var fileName = input.files[0].proper name;         alert('The file "' + fileName + '" has been selected.');                            }  </script>                              

answered December 31, 2019 at 4:43

user avatar

This culling seems the most appropriate.

                $('input[type="file"]').modify(office(due east){         var fileName = e.target.files[0].name;         alarm('The file "' + fileName +  '" has been selected.'); });                              

answered Mar 12, 2019 at 17:02

user avatar

                var filename=location.href.substr(location.href.lastIndexOf("/")+1); warning(filename);                              

user avatar

answered Dec seven, 2015 at 4:22

user avatar

Nosotros tin too remove information technology using match

                var fileName = $('input:file').val().lucifer(/[^\\/]*$/)[0]; $('#file-name').val(fileName);                              

answered Feb 11, 2020 at 11:51

user avatar

Not the answer you're looking for? Scan other questions tagged javascript jquery get filenames or ask your ain question.