admin管理员组文章数量:1389758
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
2 Answers
Reset to default 3Well, 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;
}
本文标签: htmlGet filename from input file in JavascriptStack Overflow
版权声明:本文标题:html - Get filename from input file in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744682213a2619478.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论