图片上传判断及预览脚本的效果实例_javascript技巧
style="position:relative;left:-130px;cursor:pointer;"/>
var $$ = function(id) {
return "string" == typeof id ? document.getElementById(id) : id;
};
/*
图片验证
*/
//验证上传图片格式
function checkupload(ele) {
var obj = $$("FileUploadImg");
var fileContentType = obj.value.match(/^(.*)(\.)(.{1,8})$/)[3]; //这个文件类型正则很有用:)
if (fileContentType == 'gif' || fileContentType == 'jpeg' || fileContentType == 'png' || fileContentType == 'jpg') {
//验证上传图片大小
return checkFileSize(ele);
} else {
alert('上传文件格式不正确!');
return false;
}
return false;
}
//验证上传图片大小
function checkFileSize(ele) {
var maxSize = 100 * 1024;
//ie
if (window.navigator.userAgent.indexOf("MSIE") >= 1) {
var fso;
try {
fso = new ActiveXObject('Scripting.FileSystemObject');
var file = fso.GetFile(ele.value);
//alert(file.Size);
return file.Size } catch (e) {
alert("浏览器设置禁用ActiveXObject控件,请启用");
return false;
}
}
//firefox
else if (window.navigator.userAgent.indexOf("Firefox") >= 1) {
//alert(ele.files[0].fileSize);
return ele.files[0].fileSize }
return false;
}
//加载预览图片
function previewImage(ele) {
if (ele && checkupload(ele)) {
var imgDiv = $$("imgbox");
imgDiv.innerHTML = "";
imgDiv.style.width = "100px";
imgDiv.style.height = "125px";
//ie
if (window.navigator.userAgent.indexOf("MSIE") >= 1) {
imgDiv.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod = scale)";
imgDiv.filters("DXImageTransform.Microsoft.AlphaImageLoader").src = ele.value;
}
//firefox
else if (window.navigator.userAgent.indexOf("Firefox") >= 1) {
imgDiv.innerHTML = "";
} else {
imgDiv.innerHTML = "IE/火狐浏览器才支持图片预览";
}
} else {
$$("FileUploadImg").value = "";
$$("imgbox").innerHTML = "";
//alert('重置上传控件');
}
}
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31