js获取表单上传文件的文件名和文件大小

七海恋空 2019-04-30 浏览(0 评论(0

上传:<input type="file" style="width: 500px;" onchange="fileChange(this);"/>

<script type="text/javascript">

function fileChange(target){
var isIE = /msie/i.test(navigator.userAgent) && !window.opera;//兼容IE6以上的版本
var fileSize = 0;
var fileName = null;
if(isIE && !target.files){
var filePath = target.value;
var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
var file = fileSystem.GetFile(filePath);
fileName = file.Name;
fileSize = file.Size;
}else{
fileSize = target.files[0].size;
fileName = target.files[0].name;
}
var size = fileSize / 1024 / 1024;
alert('fileSize:'+fileSize+'M');
alert('fileName:'+fileName);
}
$(document).ready(function(){

});
</script>
---------------------
作者:rain乐
来源:CSDN
原文:https://blog.csdn.net/pleasurehappy/article/details/45879635
版权声明:本文为博主原创文章,转载请附上博文链接!