// 判断文件类型的示例代码
function getFileType(file) {
// 获取文件的扩展名,toLowerCase() 方法将字符串转换为小写
const extension = file.name.split('.').pop().toLowerCase();
// 定义不同类型的文件扩展名
const imageTypes = ['jpg', 'jpeg', 'png', 'gif', 'bmp'];
const documentTypes = ['doc', 'docx', 'pdf', 'txt', 'xls', 'xlsx', 'ppt', 'pptx'];
const videoTypes = ['mp4', 'avi', 'mkv', 'mov'];
const audioTypes = ['mp3', 'wav', 'aac'];
// 根据扩展名判断文件类型
if (imageTypes.includes(extension)) {
return 'Image';
} else if (documentTypes.includes(extension)) {
return 'Document';
} else if (videoTypes.includes(extension)) {
return 'Video';
} else if (audioTypes.includes(extension)) {
return 'Audio';
} else {
return 'Unknown';
}
}
// 示例用法
const file = new File(["content"], "example.jpg", { type: "image/jpeg" });
console.log(getFileType(file)); // 输出: Image
上一篇:js 是否包含
下一篇:js 判断是数组还是对象
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站