html - Get filename from input file in Javascript - Stack Overflow

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

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

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'

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/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'
Share Improve this question edited Jun 19, 2017 at 22:42 Felipe Oriani 38.6k19 gold badges138 silver badges201 bronze badges asked Mar 17, 2015 at 19:07 hofan41hofan41 1,4681 gold badge11 silver badges25 bronze badges 5
  • 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 Commented Mar 17, 2015 at 19:10
  • In javascript, I would like to be able to reference the filename of the user selected file. – hofan41 Commented Mar 17, 2015 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 Commented Mar 17, 2015 at 19:13
  • I don't see how I could retrieve it. Could you provide a fiddle? – hofan41 Commented Mar 17, 2015 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 Commented Mar 17, 2015 at 19:33
Add a ment  | 

2 Answers 2

Reset to default 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:

  • http://jsfiddle/na4j7n6y/1/
  • How to get the file name from a full path using JavaScript?

Check this JSFiddle to reference the filepath displayed.

http://jsfiddle/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;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744682214a4587695.html

相关推荐

  • html - Get filename from input file in Javascript - Stack Overflow

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

    1天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信