input file 限制可以选择的文件

来源:互联网 发布:大数据促进旅游产业 编辑:程序博客网 时间:2024/06/05 09:05

// 只能选择图片格式的文件

<div>

<input type="file" name="file" style="float: left; width: 210px; margin-top: 4px;" multiple="" accept=".png,.jpg,.gif,.jpeg" onchange="checkFileType(this)">
<a class="upload_btn" ng-click="uploadPic(fromMark)">上传图谱</a>
</div>
<script type="text/javascript">
function checkFileType(_this) {
var $this = $(_this);
var acceptType = $this.attr('accept');

var selectedFile = $this.attr('value'); // 已选择的文件
var hz = selectedFile.substring(selectedFile.indexOf('.'), selectedFile.length);// 文件后缀

var location = acceptType.indexOf(hz);
console.log(hz + " - " + acceptType + " - " + location);
if(location > -1) {
return true;
} else {
$this.attr('value','');
alert('请选择图片格式的文件,如:' + acceptType);
return;
}
}
</script>
原创粉丝点击