欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js根據(jù)后綴判斷文件文件類型的代碼

 更新時(shí)間:2020年05月09日 21:25:24   作者:人生有戲  
這篇文章主要介紹了js根據(jù)后綴判斷文件文件類型的代碼,原來(lái)是獲取文件的擴(kuò)展名然后再判斷屬于什么類型,對(duì)于圖片多個(gè)后綴的判斷的實(shí)現(xiàn)也不是不錯(cuò)的思路,大家可以參考一下

核心代碼

<script>
function getFileType(fileName) {
  // 后綴獲取
  let suffix = '';
  // 獲取類型結(jié)果
  let result = '';
  try {
   const flieArr = fileName.split('.');
   suffix = flieArr[flieArr.length - 1];
  } catch (err) {
   suffix = '';
  }
  // fileName無(wú)后綴返回 false
  if (!suffix) { return false; }
  suffix = suffix.toLocaleLowerCase();
  // 圖片格式
  const imglist = ['png', 'jpg', 'jpeg', 'bmp', 'gif'];
  // 進(jìn)行圖片匹配
  result = imglist.find(item => item === suffix);
  if (result) {
   return 'image';
  }
  // 匹配txt
  const txtlist = ['txt'];
  result = txtlist.find(item => item === suffix);
  if (result) {
   return 'txt';
  }
  // 匹配 excel
  const excelist = ['xls', 'xlsx'];
  result = excelist.find(item => item === suffix);
  if (result) {
   return 'excel';
  }
  // 匹配 word
  const wordlist = ['doc', 'docx'];
  result = wordlist.find(item => item === suffix);
  if (result) {
   return 'word';
  }
  // 匹配 pdf
  const pdflist = ['pdf'];
  result = pdflist.find(item => item === suffix);
  if (result) {
   return 'pdf';
  }
  // 匹配 ppt
  const pptlist = ['ppt', 'pptx'];
  result = pptlist.find(item => item === suffix);
  if (result) {
   return 'ppt';
  }
  // 匹配 視頻
  const videolist = ['mp4', 'm2v', 'mkv', 'rmvb', 'wmv', 'avi', 'flv', 'mov', 'm4v'];
  result = videolist.find(item => item === suffix);
  if (result) {
   return 'video';
  }
  // 匹配 音頻
  const radiolist = ['mp3', 'wav', 'wmv'];
  result = radiolist.find(item => item === suffix);
  if (result) {
   return 'radio';
  }
  // 其他 文件類型
  return 'other';
 }
console.log(getFileType("jb51.jpg"));
</script>

在chrome中F12測(cè)試發(fā)現(xiàn)

符合我們的要求。

上面的代碼主要用到了js(=>) 箭頭函數(shù)

ES6標(biāo)準(zhǔn)新增了一種新的函數(shù):Arrow Function(箭頭函數(shù))。

為什么叫Arrow Function?因?yàn)樗亩x用的就是一個(gè)箭頭:

x => x * x

上面的箭頭函數(shù)相當(dāng)于:

function (x) {
return x * x;
}

箭頭函數(shù)相當(dāng)于匿名函數(shù),并且簡(jiǎn)化了函數(shù)定義。箭頭函數(shù)有兩種格式,一種像上面的,只包含一個(gè)表達(dá)式,連{ ... }和return都省略掉了。還有一種可以包含多條語(yǔ)句,這時(shí)候就不能省略{ ... }和return:

=>是es6語(yǔ)法中的arrow function

(x) => x + 6

相當(dāng)于

function(x){
return x + 6;
};

以上就是js根據(jù)后綴判斷文件文件類型的代碼的詳細(xì)內(nèi)容,更多關(guān)于js后綴的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論