jQuery判断文件上传类型

来源:互联网 发布:mac一般什么时候发布 编辑:程序博客网 时间:2024/06/06 13:01

http://blog.csdn.net/xpsharp/article/details/6801463

jQuery判断文件上传类型

测试兼容:火狐,chrome,IE8

使用方法:

[javascript] view plaincopyprint?
  1. $(":file").bind("change",function(){  
  2.             $(this).fileTypeJudge("package");  
  3. })  

函数:

[javascript] view plaincopyprint?
  1. /** 
  2. **author:sharp 
  3.  
  4. */  
  5.   
  6. (function($) {  
  7.     $.fn.extend({  
  8.         fileTypeJudge : function(str) {  
  9.             return this.each(function() {  
  10.                 var rightFileType;  
  11.                 var fileType;  
  12.                 var pojo;  
  13.                 if (str == "photo") {  
  14.                     rightFileType = new Array("jpg""bmp""gif""png","jpeg");  
  15.                     pojo = "图片";  
  16.                 } else if (str == "package") {  
  17.                     rightFileType = new Array("jar""six""sisx""apk","jad");  
  18.                     pojo = "游戏包";  
  19.                 } else {  
  20.                     return;  
  21.                 }  
  22.                 var fileType = $(this).val().substring($(this).val().lastIndexOf(".") + 1);  
  23.                 if (!in_array(fileType,rightFileType)) {  
  24.                     this.outerHTML += '';     
  25.                     this.value ="";   
  26.                     alert("只支持" + pojo + "文件上传!");  
  27.                 }  
  28.             })  
  29.         }  
  30.     })  
  31. })(jQuery)  
  32.   
  33. function in_array(needle, haystack) {  
  34.     // 得到needle的类型  
  35.     var type = typeof needle;  
  36.     if(type == 'string' || type =='number') {  
  37.         for(var i in haystack) {  
  38.             if(haystack[i] == needle) {  
  39.                 return true;  
  40.             }  
  41.         }  
  42.     }  
  43.     return false;  
  44. }  
0 0
原创粉丝点击