0

I have noticed that bootstrap is able to parse out and display the top level filename from a file input field when the user selects a file.

<input type="file" id="exampleFile">

JSFiddle: http://jsfiddle.net/na4j7n6y/

My question: Is this functionality exposed for me to use in my own javascript code? Or do I have to parse out the filename once again using my own technique?

I have tried retrieving the filename like so:

$('#exampleFile').val()
// Resolves to 'C:\fakepath\FILENAME' instead of 'FILENAME'
Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194
hofan41
  • 1,438
  • 1
  • 11
  • 25
  • I'm not sure what you are asking. Are you wanting to know what Bootstrap.js object holds the name of the file, so you can reference it? in HTML? in Javascript? in PHP? etc – Kirk Powell Mar 17 '15 at 19:10
  • In javascript, I would like to be able to reference the filename of the user selected file. – hofan41 Mar 17 '15 at 19:11
  • I believe the DOM would have an object element that holds the value as displayed. You could use your own JS to reference that element. – Kirk Powell Mar 17 '15 at 19:13
  • I don't see how I could retrieve it. Could you provide a fiddle? – hofan41 Mar 17 '15 at 19:17
  • My bad, looks like this feature I was speaking of was not a bootstrap feature at all, but a feature of my browser. – hofan41 Mar 17 '15 at 19:33

2 Answers2

3

Well, bootstrap is a framework based on html, css and javascript. The javascript part of this framework uses jQuery and not a own library. Then you could try to solve it using a regex with javascript/jquery, for sample:

var fullPath = $("#exampleFile").val();    
var filename = fullPath.replace(/^.*[\\\/]/, '');

Take a look here:

Community
  • 1
  • 1
Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194
1

Check this JSFiddle to reference the filepath displayed.

http://jsfiddle.net/67y9tpd4/

HTML

<div id="write"><button type="button" onclick="getAfilepath()">Print Filepath</button></div>

JS

function getAfilepath(){    
    var afilepath = document.getElementById("uploadFile").value;
    document.getElementById("write").innerHTML=afilepath;
}
Kirk Powell
  • 908
  • 9
  • 14