SoYang

思路

  1. 分离最后一个“.”,然后取后面的字符串,取得文件后缀名

  2. 后缀名进行比对

 

代码 1.

// 获取文件字符串
var file=$("input[name=\'file\']").val();
// 获取最后一个\'.\'的角标
var point = file.lastIndexOf(".");
// 抽取字符串
var type = file.substr(point);

  

代码 2.

// 获取文件字符串
var file=$("input[name=\'file\']").val(); 
// 抽取文件名字
var filename=file.replace(/.*(\/|\\)/, ""); 
// 抽取后缀名
var fileExt=(/[.]/.exec(filename)) ? /[^.]+$/.exec(filename.toLowerCase()) : \'\'; 

 

如果要进行文件类型判断, str.toLowerCase() 后在进行比对,避免大小写问题

代码

function isPicFile(fileType) {
    // 后缀名转换为小写
    var fileType = fileType.toLowerCase();
    // 创建格式数组
    var suppotFile = new Array();
    // 存储格式类型
    suppotFile[0] = "jpg";
    suppotFile[1] = "gif";
    suppotFile[2] = "bmp";
    suppotFile[3] = "png";
    suppotFile[4] = "jpeg";
    //判断fileType是否存在数组里面
    for ( var i = 0; i < suppotFile.length; i++) {
        if (suppotFile[i] == fileType) {
            return true;
        }
    }
    //如果不存在返回 false
    alert("文件类型不合法,只能是jpg、gif、bmp、png、jpeg、png类型!");
    return false;
}

  

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-18
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-28
  • 2022-12-23
  • 2022-12-23
  • 2022-02-07
  • 2021-09-22
  • 2022-12-23
相关资源
相似解决方案