1. 程式人生 > >JS 限制上傳檔案型別

JS 限制上傳檔案型別

直接上程式碼,網上看的,自己改了BUG。實測。

html程式碼

<input type="file" name="ksjh_file" class="ui_hidden" id="uploadksjh" accept="xls,xlsx" onchange="checkFileType(this)">

JS程式碼

//限制檔案上傳型別
function checkFileType(_this) {
	var $this = $(_this);
	var acceptType = $this.attr('accept');
	var selectedFile = $this.val();
	var fileType = selectedFile.substring(selectedFile.indexOf('.') + 1, selectedFile.length);
	var location = acceptType.indexOf(fileType);
	if (location > -1) {
		return true;
	} else {
		$this.attr('value', '');
		alert('請選擇'+acceptType+'格式檔案');
		return;
	}
}