日期:2014-05-16  浏览次数:20426 次

javascript判断上传图片文件类型(后缀名)

?

1、根本是正则表达式,不存在浏览器问题

?

/*
 * 判断图片类型
 * 
 * @param ths 
 * 			type="file"的javascript对象
 * @return true-符合要求,false-不符合
 */
function checkImgType(ths){
	if (ths.value == "") {
		alert("请上传图片");
		return false;
	} else {
		if (!/\.(gif|jpg|jpeg|png|GIF|JPG|PNG)$/.test(ths.value)) {
			alert("图片类型必须是.gif,jpeg,jpg,png中的一种");
			ths.value = "";
			return false;
		}
	}
	return true;
}

?

?

2、以下判断像素的在firefox下不行,主要是这句代码document.body.insertAdjacentElement("beforeEnd", img);

?

暂时未找到解决办法....

?

/*
 * 判断图片大小
 * 
 * @param ths 
 * 			type="file"的javascript对象
 * @param width
 * 			需要符合的宽 
 * @param height
 * 			需要符合的高
 * @return true-符合要求,false-不符合
 */
function checkImgPX(ths, width, height) {
	var img = null;
 	img = document.createElement("img");
	document.body.insertAdjacentElement("beforeEnd", img); // firefox不行
	img.style.visibility = "hidden"; 
	img.src = ths.value;
	var imgwidth = img.offsetWidth;
	var imgheight = img.offsetHeight;
	
	alert(imgwidth + "," + imgheight);
	
	if(imgwidth != width || imgheight != height) {
		alert("图的尺寸应该是" + width + "x"+ height);
		ths.value = "";
		return false;
	}
	return true;
}

?

求解....

求解....

?

?

ths.value = "";  // IE无效

..讨厌的浏览器兼容问题!

?

.