代码很简单,常见的图片格式均已加入验证之中,小伙伴们可以直接拿去用的。
废话少说,直接上代码
代码如下:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "https://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script src="jquery-1.7.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#form01").change( function(){
var filepath=$("input[name='myfile']").val();
var extstart=filepath.lastindexof(".");
var ext=filepath.substring(extstart,filepath.length).touppercase();
if(ext!=".bmp"&&ext!=".png"&&ext!=".gif"&&ext!=".jpg"&&ext!=".jpeg"){
alert("图片限于bmp,png,gif,jpeg,jpg格式");
return false;
}else{$("#name01").text(ext)}
var file_size = 0;
if ( $.browser.msie) {
var img=new image();
img.src=filepath;
while(true){
if(img.filesize > 0){
if(img.filesize>3*1024*1024){
alert("图片不大于100mb。");
}else{
var num03 = img.filesize/1024;
num04 = num03.tofixed(2)
$(".size02").text(num04+"kb");
}
break;
}
}
} else {
file_size = this.files[0].size;
console.log(file_size/1024/1024 + " mb");
var size = file_size / 1024;
if(size > 10240){
alert("上传的文件大小不能超过10m!");
}else{
var num01 = file_size/1024;
num02 = num01.tofixed(2)
$("#size01").text(num02 + " kb");
}
}
return true;
});
});
</script>
<title>无标题文档</title>
</head>
<body>
<table width="500" cellspacing="0" cellpadding="0">
<tr>
<td width="72" id="name01"> </td>
<td width="242"><input type="file" name="myfile" id="form01" /></td>
<td width="184" id="size01" class="size02"> </td>
</tr>
</table>
</body>
</html>
如对本文有疑问, 点击进行留言回复!!
150Vue-Router路由概述+基本使用router-view占位符+重定向redirect
网友评论