1. function getFileType(fileName) {
    2. // 后缀获取
    3. let suffix = '';
    4. // 获取类型结果
    5. let result = '';
    6. try {
    7. const flieArr = fileName.split('.');
    8. suffix = flieArr[flieArr.length - 1];
    9. } catch (err) {
    10. suffix = '';
    11. }
    12. // fileName 无后缀返回 false
    13. if (!suffix) {
    14. return false;
    15. }
    16. suffix = suffix.toLocaleLowerCase();
    17. // 图片格式
    18. const imglist = ['png', 'jpg', 'jpeg', 'bmp', 'gif'];
    19. // 进行图片匹配
    20. result = imglist.find(item => item === suffix);
    21. if (result) {
    22. return 'image';
    23. }
    24. // 匹配 txt
    25. const txtlist = ['txt'];
    26. result = txtlist.find(item => item === suffix);
    27. if (result) {
    28. return 'txt';
    29. }
    30. // 匹配 excel
    31. const excelist = ['xls', 'xlsx'];
    32. result = excelist.find(item => item === suffix);
    33. if (result) {
    34. return 'excel';
    35. }
    36. // 匹配 word
    37. const wordlist = ['doc', 'docx'];
    38. result = wordlist.find(item => item === suffix);
    39. if (result) {
    40. return 'word';
    41. }
    42. // 匹配 pdf
    43. const pdflist = ['pdf'];
    44. result = pdflist.find(item => item === suffix);
    45. if (result) {
    46. return 'pdf';
    47. }
    48. // 匹配 ppt
    49. const pptlist = ['ppt', 'pptx'];
    50. result = pptlist.find(item => item === suffix);
    51. if (result) {
    52. return 'ppt';
    53. }
    54. // 匹配 视频
    55. const videolist = ['mp4', 'm2v', 'mkv', 'rmvb', 'wmv', 'avi', 'flv', 'mov', 'm4v'];
    56. result = videolist.find(item => item === suffix);
    57. if (result) {
    58. return 'video';
    59. }
    60. // 匹配 音频
    61. const radiolist = ['mp3', 'wav', 'wmv'];
    62. result = radiolist.find(item => item === suffix);
    63. if (result) {
    64. return 'radio';
    65. }
    66. // 其他 文件类型
    67. return 'other';
    68. }