admin管理员组文章数量:1134232
I've seen similar questions before, which ends up with no solution, because of security reasons.
But today I see hostmonster has successfully implemented this when I opened a ticket and attached a file in their backend.
It works both with firefox and IE(version 8 to be exact).
I've verified it's exactly client side scripting; no request is sent(with firebug).
I've seen similar questions before, which ends up with no solution, because of security reasons.
But today I see hostmonster has successfully implemented this when I opened a ticket and attached a file in their backend.
It works both with firefox and IE(version 8 to be exact).
I've verified it's exactly client side scripting; no request is sent(with firebug).
Share Improve this question edited Aug 24, 2024 at 14:05 danronmoon 3,8735 gold badges35 silver badges58 bronze badges asked Feb 3, 2010 at 4:06 user198729user198729 63.6k109 gold badges255 silver badges352 bronze badges 1- What do you mean by "get a file name"? Isn't the file name obvious, because the user just selected it? – John Feminella Commented Feb 3, 2010 at 4:11
7 Answers
Reset to default 143You can get the file name, but you cannot get the full client file-system path.
Try to access to the value
attribute of your file input
on the change
event.
Most browsers will give you only the file name, but there are exceptions like IE8 which will give you a fake path like: "C:\fakepath\myfile.ext"
and older versions (IE <= 6) which actually will give you the full client file-system path (due its lack of security).
document.getElementById('fileInput').onchange = function () {
alert('Selected file: ' + this.value);
};
You can use the next code:
JS
function showname () {
var name = document.getElementById('fileInput');
alert('Selected file: ' + name.files.item(0).name);
alert('Selected file: ' + name.files.item(0).size);
alert('Selected file: ' + name.files.item(0).type);
};
HTML
<body>
<p>
<input type="file" id="fileInput" multiple onchange="showname()"/>
</p>
</body>
just tested doing this and it seems to work in firefox & IE
<html>
<head>
<script type="text/javascript">
function alertFilename()
{
var thefile = document.getElementById('thefile');
alert(thefile.value);
}
</script>
</head>
<body>
<form>
<input type="file" id="thefile" onchange="alertFilename()" />
<input type="button" onclick="alertFilename()" value="alert" />
</form>
</body>
</html>
I'll answer this question via Simple Javascript that is supported in all browsers that I have tested so far (IE8 to IE11, Chrome, FF etc).
Here is the code.
function GetFileSizeNameAndType()
{
var fi = document.getElementById('file'); // GET THE FILE INPUT AS VARIABLE.
var totalFileSize = 0;
// VALIDATE OR CHECK IF ANY FILE IS SELECTED.
if (fi.files.length > 0)
{
// RUN A LOOP TO CHECK EACH SELECTED FILE.
for (var i = 0; i <= fi.files.length - 1; i++)
{
//ACCESS THE SIZE PROPERTY OF THE ITEM OBJECT IN FILES COLLECTION. IN THIS WAY ALSO GET OTHER PROPERTIES LIKE FILENAME AND FILETYPE
var fsize = fi.files.item(i).size;
totalFileSize = totalFileSize + fsize;
document.getElementById('fp').innerHTML =
document.getElementById('fp').innerHTML
+
'<br /> ' + 'File Name is <b>' + fi.files.item(i).name
+
'</b> and Size is <b>' + Math.round((fsize / 1024)) //DEFAULT SIZE IS IN BYTES SO WE DIVIDING BY 1024 TO CONVERT IT IN KB
+
'</b> KB and File Type is <b>' + fi.files.item(i).type + "</b>.";
}
}
document.getElementById('divTotalSize').innerHTML = "Total File(s) Size is <b>" + Math.round(totalFileSize / 1024) + "</b> KB";
}
<p>
<input type="file" id="file" multiple onchange="GetFileSizeNameAndType()" />
</p>
<div id="fp"></div>
<p>
<div id="divTotalSize"></div>
</p>
*Please note that we are displaying filesize in KB (Kilobytes). To get in MB divide it by 1024 * 1024 and so on*.
It'll perform file outputs like these on selecting
This is based on a comment under the accepted answer, but easiest might be:
$(".fileUploader").on("change", function(e){
console.log(e.target.files[0].name);
})
Or iterate if you have mutliple file selection enabled on that input. Assumes <input class="fileUploader" ....>
.
To clean up the C:\fakepath\
I add .replace('C:\\fakepath\\', ' ')
to replace it with an empty string
This works clean for me, no C:\fakepath\
:)
document.getElementById('fileInput').onchange = function () {
alert('Selected file: ' + this.files[0].name);
};
版权声明:本文标题:javascript - How to get file name when user selects a file via <input type="file" >? - Stack Ove 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736820248a1954254.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论