【web】获取文件类型

来源:互联网 发布:java redis作用 编辑:程序博客网 时间:2024/06/07 02:30

获取文件类型


/* * param  fileNamewewe.png */     var fileObject = {getFileType : function(fileName){//取文件类型if(fileName.indexOf(".")<0){return console.log("文件格式不符!");}//substr抽取文件后缀名var last=fileName.substr(fileName.lastIndexOf("."));//last:   .xx  .  .zipif(last=='.'){return console.log("文件格式不符!");}last=last.substr(1);//把.不要//console.log("文件类型===>"+last);return last;},getFileName : function(fileName){//取文件名if(fileName.indexOf(".")<0){return console.log("文件格式不符!");}var stringArray = fileName.split(".");return stringArray[0];}};    var file_type = fileObject.getFileType(file_name);    var getfilename = fileObject.getFileName(file_name);    console.log("文件类型是:" + file_type);


备注一些api注释

indexOf 把一个字符串从左往右数,检索指定的符号(字符串),返回该符号在此字符串中出现的位置(下标)。

http://www.w3school.com.cn/jsref/jsref_indexOf.asp

使用方法:字符串变量.indexOf(需要被检索的字符串);

 

lastIndexOf indexOf的用法和返回值是一样的,不同的是,从右向左数。

 

substr 传入起始索引号,返回提取字符串中指定数目的字符,返回一个新的字符串

http://www.w3school.com.cn/jsref/jsref_substr.asp

使用方法:字符串变量.sbustr(字符串的下标数值);

 

slice 方法可提取字符串的某个部分,并以新的字符串返回被提取的部分

http://www.w3school.com.cn/jsref/jsref_slice_string.asp

使用方法:字符串变量.slice(字符串的下标数值);

 

split 把一个字符串以指定的某个符号,切分字符串,返回一个字符串数组。

http://www.w3school.com.cn/jsref/jsref_split.asp

使用方法:字符串变量.split(以此字符串作为切分点);





0 0