Determine the format type of the imported file mp4

Reference Code: 

function submit_precheck(){
	var import_file = $("#imp_file").val();
	if (import_file == undefined || import_file == null || import_file == ''){
		swal('提示', '请选择待导入的JSON文件!','warning');
		return false;
	}
	var idx = import_file.indexOf(".json");
	if(idx < 1){
		swal('提示', '所选文件的格式不是JSON格式!','warning');
		return false;
	}
	swal({
		  title: "模型导入",
		  text: "请确认是否要批量导入模型数据?",
		  type: "warning",
		  showCancelButton: true,
		  confirmButtonColor: "#DD6B55",
		  confirmButtonText: "Yes",
		  closeOnConfirm: true,
		},
		function(isConfirm){
			if (isConfirm == true){
				$("#pm_dtpexp_remark").val("开始导入,请等待...\n");
				request_import();
			} 
			else {
				$("#pm_dtpexp_remark").val("导入取消!");
			}
		}
	);	
	return false;
}

Actual code: 

function submit_precheck(){
	var import_file = $("#imp_file").val();
	if (import_file == undefined || import_file == null || import_file == ''){
		swal('提示', '请选择待导入的视频文件!','warning');
		return false;
	}
	var idx = import_file.lastIndexOf(".");
	//获取后缀
	var ext = import_file.substr(idx+1);
	if(!isAssetTypeAnImage(ext)){
		swal('提示', '所选文件的格式不是MP4格式!','warning');
		return false;
	}
	var files = $('#imp_file')[0].files;  //console.log(files);
	//获取文件名
	var name  = files[0].name;
	var filename = name.substring(0, name.lastIndexOf("."))
	$("#text_docsinfo_docsfilename").val(filename);
	$("#text_docsinfo_projectid").val(g_appm_md_projectid);
	swal({
		  title: "视频导入",
		  text: "请确认是否要导入视频数据?",
		  type: "warning",
		  showCancelButton: true,
		  confirmButtonColor: "#DD6B55",
		  confirmButtonText: "Yes",
		  closeOnConfirm: true,
		},
		function(isConfirm){
			if (isConfirm == true){
				$("#pm_dtpexp_remark").val("开始导入,请等待...\n");
				request_import();
			} 
			else {
				$("#pm_dtpexp_remark").val("导入取消!");
			}
		}
	);	
	return false;
}

1. Get the file suffix

//文件路径
var filePath = "file://upload/jb51.png";
//获取最后一个.的位置
var index= filePath.lastIndexOf(".");
//获取后缀
var ext = filePath.substr(index+1);
//输出结果
console.log(ext);

2. File type judgment

function isAssetTypeAnImage(ext) {
     return ['png', 'jpg', 'jpeg', 'bmp', 'gif', 'webp', 'psd', 'svg', 'tiff'].indexOf(ext.toLowerCase()) !== -1;
}


//文件路径
var filePath = "file://upload/jb51.png";
//获取最后一个.的位置
var index= filePath.lastIndexOf(".");
//获取后缀
var ext = filePath.substr(index+1);
//判断是否是图片
console.log("该文件是否为图片:" + isAssetTypeAnImage(ext));

 

Guess you like

Origin blog.csdn.net/ZHOU_VIP/article/details/113863406