文件操作常用方法集合
常用文件方法,直接复制使用,避免重复工作. 不断搜集更新~
- 文件类型判断
常用文件类型:jpg、jpeg、png、gif、bmp、pdf、doc、docx、xls、xlsx、ppt、pptx
function checkFileType(fileName) {
let fileExtension = fileName.split('.').pop().toLowerCase();
let fileTypes = {
jpg: 'image/jpeg',
jpeg: 'image/jpeg',
png: 'image/png',
gif: 'image/gif',
bmp: 'image/bmp',
pdf: 'application/pdf',
doc: 'application/msword',
docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
xls: 'application/vnd.ms-excel',
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
ppt: 'application/vnd.ms-powerpoint',
pptx: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
};
if (fileTypes[fileExtension]) {
return fileTypes[fileExtension];
} else {
return false;
}
}
input文件上传
function onChange(file) {
const { type } = file;
// 判断是否是docx类型
if (type === checkFileType('.docx')) {
// 是.docx类型 do sth.
}
}