DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> js判斷上傳文件的類型和大小示例代碼
js判斷上傳文件的類型和大小示例代碼
編輯:關於JavaScript     
復制代碼 代碼如下:
//檢測文件大小和類型
function fileChange(target){
//檢測上傳文件的類型
if(!(/(?:jpg|gif|png|jpeg)$/i.test(target.value))) {
alert("只允許上傳jpg|gif|png|jpeg格式的圖片");
if(window.ActiveXObject) {//for IE
target.select();//select the file ,and clear selection
document.selection.clear();
} else if(window.opera) {//for opera
target.type="text";target.type="file";
} else target.value="";//for FF,Chrome,Safari
return;
} else {
return; //alert("ok");//or you can do nothing here.
}

//檢測上傳文件的大小
var isIE = /msie/i.test(navigator.userAgent) && !window.opera;
var fileSize = 0;
if (isIE && !target.files){
var filePath = target.value;
var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
var file = fileSystem.GetFile (filePath);
fileSize = file.Size;
} else {
fileSize = target.files[0].size;
}
var size = fileSize / 1024;
if(size>(500)){
alert("文件大小不能超過500KB");
if(window.ActiveXObject) {//for IE
target.select();//select the file ,and clear selection
document.selection.clear();
} else if(window.opera) {//for opera
target.type="text";target.type="file";
} else {
target.value="";//for FF,Chrome,Safari
}
return;
}else{
return;
}
}
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved