复制代码 代码如下:
//检测文件大小和类型
function fileChange(target){
//检测上传文件的类型
if(!(/(?:jpg|gif|png|jpeg)$/i.test(target.value))) {
alert("只允许上传jpg|gif|png|jpeg格式的图片");
if(window.ActiveXObject) {//for IE
target.select();//select the file ,and clear selection
document.selection.clear();
} else if(window.opera) {//for opera
target.type="text";target.type="file";
} else target.value="";//for FF,Chrome,Safari
return;
} else {
return; //alert("ok");//or you can do nothing here.
}
//检测上传文件的大小
var isIE = /msie/i.test(navigator.userAgent) && !window.opera;
var fileSize = 0;
if (isIE && !target.files){
var filePath = target.value;
var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
var file = fileSystem.GetFile (filePath);
fileSize = file.Size;
} else {
fileSize = target.files[0].size;
}
var size = fileSize / 1024;
if(size>(500)){
alert("文件大小不能超过500KB");
if(window.ActiveXObject) {//for IE
target.select();//select the file ,and clear selection
document.selection.clear();
} else if(window.opera) {//for opera
target.type="text";target.type="file";
} else {
target.value="";//for FF,Chrome,Safari
}
return;
}else{
return;
}
}
//检测文件大小和类型
function fileChange(target){
//检测上传文件的类型
if(!(/(?:jpg|gif|png|jpeg)$/i.test(target.value))) {
alert("只允许上传jpg|gif|png|jpeg格式的图片");
if(window.ActiveXObject) {//for IE
target.select();//select the file ,and clear selection
document.selection.clear();
} else if(window.opera) {//for opera
target.type="text";target.type="file";
} else target.value="";//for FF,Chrome,Safari
return;
} else {
return; //alert("ok");//or you can do nothing here.
}
//检测上传文件的大小
var isIE = /msie/i.test(navigator.userAgent) && !window.opera;
var fileSize = 0;
if (isIE && !target.files){
var filePath = target.value;
var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
var file = fileSystem.GetFile (filePath);
fileSize = file.Size;
} else {
fileSize = target.files[0].size;
}
var size = fileSize / 1024;
if(size>(500)){
alert("文件大小不能超过500KB");
if(window.ActiveXObject) {//for IE
target.select();//select the file ,and clear selection
document.selection.clear();
} else if(window.opera) {//for opera
target.type="text";target.type="file";
} else {
target.value="";//for FF,Chrome,Safari
}
return;
}else{
return;
}
}
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com
暂无“js判断上传文件的类型和大小示例代码”评论...