您的位置:首页 > Web前端 > JQuery

jQuery 判断文件上传类型

2011-09-22 17:55 288 查看
jQuery判断文件上传类型

测试兼容:火狐,chrome,IE8

使用方法:

$(":file").bind("change",function(){
$(this).fileTypeJudge("package");
})


函数:

/**
**author:sharp

*/

(function($) {
$.fn.extend({
fileTypeJudge : function(str) {
return this.each(function() {
var rightFileType;
var fileType;
var pojo;
if (str == "photo") {
rightFileType = new Array("jpg", "bmp", "gif", "png","jpeg");
pojo = "图片";
} else if (str == "package") {
rightFileType = new Array("jar", "six", "sisx", "apk","jad");
pojo = "游戏包";
} else {
return;
}
var fileType = $(this).val().substring($(this).val().lastIndexOf(".") + 1);
if (!in_array(fileType,rightFileType)) {
this.outerHTML += '';
this.value ="";
alert("只支持" + pojo + "文件上传!");
}
})
}
})
})(jQuery)

function in_array(needle, haystack) {
// 得到needle的类型
var type = typeof needle;
if(type == 'string' || type =='number') {
for(var i in haystack) {
if(haystack[i] == needle) {
return true;
}
}
}
return false;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: