Skip to content

函数: getFileType()

ts
function getFileType(fileName): 
  | "audio"
  | "video"
  | "txt"
  | "pdf"
  | "zip"
  | "other"
  | "image"
  | "excel"
  | "word";

定义于: file.ts:18

根据文件名后缀判断其所属的文件分类。

内部按后缀映射到 imagetxtexcelwordpdfvideoaudiozip 等分类, 命中即返回对应分类名;未命中任何分类(含无后缀、空值)统一返回 'other'

参数

参数类型描述
fileNamestring文件名,可带查询参数(如 a.png?v=1)。

返回值

| "audio" | "video" | "txt" | "pdf" | "zip" | "other" | "image" | "excel" | "word"

分类名,取值为 'image' | 'txt' | 'excel' | 'word' | 'pdf' | 'video' | 'audio' | 'zip' | 'other';无法识别或无后缀时返回 'other'

备注

判断前会先去掉 ?查询参数,并将后缀转为小写后再匹配。需要拿到扩展名本身时可用 getFileExt

示例

ts
getFileType('132546.png')   // => 'image'
getFileType('a.mp3')        // => 'audio'
getFileType('a.PNG?v=1')    // => 'image'(自动去查询参数、后缀小写)
getFileType('README')       // => 'other'(无后缀)