Loading

JS根据文件名获取文件类型

/** 文件识别 */
export const getFileType = (fileName: string) => {
  if (!fileName) return 'other';
  //根据文件名提取后缀名
  const index = fileName.lastIndexOf('.');
  const ext = fileName.substr(index + 1).toLowerCase();
  const enumsFileType: Record<string, any> = {
    image: ['png', 'jpg', 'jpeg', 'gif', 'webp', 'bmp', 'svg', 'tiff'],
    pdf: ['pdf'],
    excel: ['xlsx'],
    word: ['docx', 'doc'],
    ppt: ['ppt', 'pptx'],
    video: ['mp4', 'avi', 'mov']
  };
  for (const key in enumsFileType) {
    if (enumsFileType[key].includes(ext)) {
      return key;
    }
  }
  return 'other';
};

 

posted @ 2024-07-12 16:23  冯叶青  阅读(2)  评论(0编辑  收藏  举报