var upImg_config = { e0:'上傳出錯,請重試!', e2:'今日上傳超過限制次數', e3:'照片格式不符合要求!', e4:'上傳出錯,請重試!', e5:'照片大小超過限制!', e6:'照片不能小於0M!' } //檢查是什麼瀏覽器 function getExplorer() { var explorer = window.navigator.userAgent ; if (explorer.indexOf("MSIE") >= 0) {//ie return "ie"; }else if (explorer.indexOf("Firefox") >= 0) {//firefox return "Firefox"; }else if(explorer.indexOf("Chrome") >= 0){//Chrome return "Chrome"; }else if(explorer.indexOf("Opera") >= 0){//Opera return "Opera"; }else if(explorer.indexOf("Safari") >= 0){//Safari return "Safari"; } } //獲取選擇的文件的路徑 function getObjectURL(target){ var url = null; if (window.navigator.userAgent.indexOf("MSIE")>=1){ target.select(); url = document.selection.createRange().text; } else { var file = $(target)[0].files[0]; if (window.createObjectURL != undefined) { url = window.createObjectURL(file) } else if (window.URL != undefined) { url = window.URL.createObjectURL(file) } else if (window.webkitURL != undefined) { url = window.webkitURL.createObjectURL(file) } } return url; } // 獲取文件名稱後綴 、不帶後綴 function getFilePath(filePath){ var path = []; var pos = filePath.lastIndexOf('.'); path['fileExt'] = filePath.substring(pos); //獲取後綴 path['fileName'] = filePath.substring(0,pos);//獲取文件名,不帶後綴 return path; } //上傳圖片 $(".showPhotosCon dt").click(function(){ $(".upTips").hide(); $(".upLoad").removeClass("hide"); $(".shade").removeClass("hide"); }); //選擇圖片 判斷格式、大小等 $("#fileUpload").on('change',function(){ var _file = $(this); var fileSize = 0; var filetypes =[".jpg",".gif",".png",".bmp"]; var filemaxsize = 1024*1024*2;//2M var filepath = _file.val(); $(".upTips").hide(); if(filepath){ //getObjectURL(this); var pathArr = getFilePath(filepath); console.log(pathArr); if(filetypes && filetypes.length>0){ if($.inArray(pathArr['fileExt'].toLowerCase(),filetypes) < 0){ $(".upTips").html(upImg_config.e2).show(); //清除input內容 _file.after(_file.clone().val("")); _file.remove(); // if(myExplorer == 'ie'){ // $(this).focus(); // this.select(); // document.execCommand("delete"); // }else{ // $(this).val(""); // } return false; } } var myExplorer = getExplorer(); if(myExplorer == 'ie'){ var img = new Image(); img.src = filepath; fileSize = img.fileSize > 0?img.fileSize:500; }else{ fileSize = $(this)[0].files[0].size || $(this)[0].files[0].fileSize; } if(fileSize>filemaxsize || fileSize <= 0){ if(fileSize>filemaxsize){ $(".upTips").html(upImg_config.e5).show(); }else{ $(".upTips").html(upImg_config.e6).show(); } //清除input內容 _file.after(_file.clone().val("")); _file.remove(); return false; } }else{ return false; } uploadSubmit();// 直接上傳。。 }); //執行上傳操做 function uploadSubmit(){ //alert(11); var btn = $('.upPicShade'); btn.html("上傳中..."); $.ajaxFileUpload({ url:wm_config.uploadImg, secureuri:false, fileElementId:'fileUpload', dataType:'json', data:{}, success:function(data,status){ // 固定的前置錯誤碼 var test1 = "HTTP Status 404 - No result defined for action com.henda.webma.action.MapUploadJsonAction and result"; var obj = new Object(); if (data.length > test1.length) { // 去掉先後空格 var test2 = data.substring(test1.length).replace(/^\s+|\s+$/g, ""); // 將Json串轉爲對象 obj = eval("(" + test2 + ")"); } if (obj.status == "success") { //0失敗 1 成功 2 限制 3 不規範 uploadImgCallBack(obj.message); } else { // 若是錯了 if (typeof (obj.message) != undefined) { uploadImgCallBack(4); if (obj.message != '') { //alert(obj.message); } else { //alert("faile"); } } } }, error:function(data,status,e){ //console.log(e); //alert(e); uploadImgCallBack(4); } }); } //上傳後回調函數 function uploadImgCallBack(code){ switch(code){ case 0: $(".upTips").html(upImg_config.e0).show(); break; case 1: $(".upLoad").addClass("hide"); return false; break; case 2: $(".upTips").html(upImg_config.e2).show(); break; case 3: $(".upTips").html(upImg_config.e3).show(); break; case 4: $(".upTips").html(upImg_config.e4).show(); break; default: break; } var btn = $('.upPicShade'); btn.html("選擇照片");}