博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JQuery 判断文件类型
阅读量:4037 次
发布时间:2019-05-24

本文共 1437 字,大约阅读时间需要 4 分钟。

 
//文件上传文件选择后事件
 
$(document).ready(
function
() {
  
$(
"input[name^='fileName']"
).each(
//这里是用了each因为是多文件上传,input的id都是fileToUpload  开头
    
function
() {
 
$(
"#" 
+ $(
this
).attr(
"name"
) + 
""
).live(
'change'
,
function
() {
 
var 
fileName = $(
this
).val();
 
if 
(fileName != 
null
&& fileName != 
""
) {
  
//lastIndexOf如果没有搜索到则返回为-1
  
if 
(fileName.lastIndexOf(
"."
) != -1) {
   
var 
fileType = (fileName.substring(fileName.lastIndexOf(
"."
) + 1,
  
fileName.length)).toLowerCase();
   
var 
suppotFile = 
new 
Array();
   
suppotFile[0] = 
"jpg"
;
   
suppotFile[1] = 
"gif"
;
   
suppotFile[2] = 
"bmp"
;
   
suppotFile[3] = 
"png"
;
   
suppotFile[4] = 
"jpeg"
;
   
for 
var 
i = 0; i < suppotFile.length; i++) {
    
if 
(suppotFile[i] == fileType) {
if 
(fileName.length > 100) {
 
alert(
"文件名长度不能超过100字符"
);
 
if 
(!window.addEventListener) {      
    
document.getElementById(fileName[j]).outerHTML+=
''
;  
//IE清除inputfile
    
}
else 
     
document.getElementById(fileName[j]).value = 
""
;   
//FF清除inputfile
    
 
return 
false
;
}
return 
true
;
 
else 
{
continue
;
 
}
 
}
  
alert(
"文件类型不合法,只支持 jpg、gif、png、jpeg类型!"
);
    
if 
(!window.addEventListener) {      
    
document.getElementById(fileName[j]).outerHTML+=
''
;  
//IE
    
}
else 
     
document.getElementById(fileName[j]).value = 
""
;   
//FF
    
   
return 
false
;
  
else 
{
   
alert(
"文件类型不合法,只支持 jpg、gif、png、jpeg类型!"
);
    
if 
(!window.addEventListener) {      
    
document.getElementById(fileName[j]).outerHTML+=
''
;  
//IE
    
}
else 
     
document.getElementById(fileName[j]).value = 
""
;   
//FF
    
   
return 
false
;
  
}
 
}
});
});
});

转载地址:http://lfjdi.baihongyu.com/

你可能感兴趣的文章
Visual Studio 2010:C++0x新特性
查看>>
drwtsn32.exe和adplus.vbs进行dump文件抓取
查看>>
cppcheck c++静态代码检查
查看>>
在C++中使用Lua
查看>>
C++中使用Mongo执行count和distinct运算
查看>>
一些socket的编程经验
查看>>
socket编程中select的使用
查看>>
关于AIS编码解码的两个小问题
查看>>
GitHub 万星推荐:黑客成长技术清单
查看>>
可以在线C++编译的工具站点
查看>>
关于无人驾驶的过去、现在以及未来,看这篇文章就够了!
查看>>
所谓的进步和提升,就是完成认知升级
查看>>
昨夜今晨最大八卦终于坐实——人类首次直接探测到了引力波
查看>>
如何优雅、机智地和新公司谈薪水?
查看>>
为什么读了很多书,却学不到什么东西?
查看>>
长文干货:如何轻松应对工作中最棘手的13种场景?
查看>>
如何用好碎片化时间,让思维更有效率?
查看>>
No.147 - LeetCode1108
查看>>
No.174 - LeetCode1305 - 合并两个搜索树
查看>>
No.175 - LeetCode1306
查看>>