图片上传之前检查大小、尺寸、格式并预览的js代码
下面的代码,需要保存到本地种执行,在线看不到实际效果。
validate before upload by Lance Zhang
File type :gif,jpg,jpeg,png,bmp
File Size limit:
K
Image Height Limit:
Image Width Limit:
Image Preview
function CheckFileSize()
{
var limit = document.getElementById("fileSizeLimit").value * 1024;
var width = document.getElementById("widthLimit").value ;
var height = document.getElementById("heightLimit").value;
if (oFileChecker.fileSize > limit)
{
alert("FileSize :"+oFileChecker.fileSize+"byte--too large!");
}
else
{
alert("FileSize :"+oFileChecker.fileSize+"byte--ok");
}
if( oFileChecker.height> height)
{
alert("ImageHeight :"+oFileChecker.height+"--too height!");
}
else
{
alert("ImageHeight :"+oFileChecker.height+"--ok");
}
if(oFileChecker.width> width)
{
alert("ImageWidth :"+oFileChecker.width+"--too width!");
}
else
{
alert("ImageWidth :"+oFileChecker.width+"--ok");
}
return false;
}
var right_type=new Array(".gif",".jpg",".jpeg",".png",".bmp");
var oFileChecker = document.getElementById("fileChecker");
function changeSrc(filePicker)
{
if(!checkImgType(filePicker.value))
{
alert("the file type is not correct");
return;
}
oFileChecker.src = filePicker.value;
}
oFileChecker.onreadystatechange = function ()
{
if (oFileChecker.readyState == "complete")
{
CheckFileSize();
}
}
function checkImgType(fileURL)
{
var right_typeLen=right_type.length;
var imgUrl=fileURL.toLowerCase();
var postfixLen=imgUrl.length;
var len4=imgUrl.substring(postfixLen-4,postfixLen);
var len5=imgUrl.substring(postfixLen-5,postfixLen);
for (i=0;i
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]