Js上傳圖片,相容ie

來源:互聯網
上載者:User

標籤:

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("選擇照片");
}

 

Js上傳圖片,相容ie

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.