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';
};
愿你走出半生,归来仍是少年

浙公网安备 33010602011771号