js判断图片大小与图片类型<script language="javascript">
function checkinput(){
var sUploadAllowedExtensions = ".gif .jpg .jpeg .png" ;
var sExt = document.file_upload.file.value.match( /.[^.]*$/ ) ;
sExt = sExt ? sExt[0].toLowerCase() : "." ;
if (document.file_upload.file.value==""){
alert("文件路径不能为空!");
return false;
}
if ( sUploadAllowedExtensions.indexOf( sExt ) < 0 )
{
alert("对不起, 只有下面的图片格式才能上传:nn" + sUploadAllowedExtensions + "nn请重新选择.") ;
return false;
}
var ipreview = document.all('ipreview')
if (ipreview.fileSize>10240){
alert('您选择的图片尺寸大于'+Math.round(10240/1024)+'KB,不能上传');
return false;
//myddd.disabled=true;
}
return true;
}
function isImage(file){
//Get a file extension
var ext = file.substr(file.lastIndexOf('.')).toLowerCase()
//Check extension to image types.
return '.gif,.jpg,.png,.jpeg,.ico,'.indexOf(ext+',') >= 0
};
var lastfieldname = ''
function preview(i) {
//get current input preview
var file = i.value
if (file.length<=0) return;
//or get get preview for one of form field
//var file = file_upload.Sourcefile.value
var ipreview = document.all('ipreview')
if (isImage(file)) {
//Show preview for the image.
ipreview.src = 'file://' + file
//alert(ipreview.fileSize);
eval("file_upload.fileSize1.value=ipreview.fileSize/1024;");
//ImageName.innerHTML = '图片预览<br>(' + i.name + ')'
if (ipreview.fileSize>10240){
alert('您选择的图片尺寸大于'+Math.round(10240/1024)+'KB,不能上传');
//myddd.disabled=true;
}
} else {
//some default image for preview
ipreview.src = 'res://shdoclc.dll/warning.gif'
if (lastfieldname != i.name) alert('请选择图片文件 (.gif,.jpg,.png)');
};
lastfieldname = i.name
}
window.onerror = donotmsgboxes;
function donotmsgboxes(msg,url,line)
{ // we do not need error messages
return true
}
</script>