jquery ajax form 提交圖片 文件


    function submitImg(imgid,formid,imginfo){
    var filepath=$("#"+imgid).val(); 
        var extStart=filepath.lastIndexOf(".");
        if(extStart!=-1){ 
        var ext=filepath.substring(extStart,filepath.length).toUpperCase();
        if(ext!=".PNG"&&ext!=".GIF"&&ext!=".JPG"&&ext!=".JPEG"&&ext!=".jpg"&&ext!=".png"&&ext!=".jpeg"&&ext!=".gif"){
         $("#"+imginfo).html("上傳文件必須為 png,gif,jpeg,jpg格式").css("clour","red").css("size","14px");        
         return false;
        }else{
         $("#"+imginfo).html("");
        }
    }
   var options = {                
               url:action="<%=request.getContextPath()%>/customer/uploadBus",//后台的處理,也就是form里的action  
               type:"POST",  
               dataType:"json", //數據格式,有XML,html,json,默認為文本                
               success:function(msg){  
             if (msg != "" && msg != null) {
                    if (msg.code == 0) {
                                    $.cookie(imgid, msg.url, {
                                        expires : 1000*60*60,
                                        path : '/'
                                    });
                                    $("#"+imginfo).html("身份證正面上傳成功").css(
                                            "color", "#090");
                                } else {
                                    $("#"+imginfo).html("上傳的圖片不能大於2M").css(
                                            "color", "red");
                                }
                            } else {
                                $("#"+imginfo).html("圖片服務器網絡異常").css("color","red");
                                }
                                
               }  
            };   
         $("#"+formid).ajaxSubmit(options);  
         return false;//為了防止刷新  
//});
}

html代碼

<span class="STYLE21">
                    <form id="uploadBus" action="<%=request.getContextPath()%>/customer/uploadBus" method="post" enctype="multipart/form-data">
                        <input type="file" id="business_address" name="business_address" accept="image/*" onchange="submitImg('business_address','uploadBus','imginfo1')"/>
                    </form>
                </span><span id="imginfo1" style="color: red;"></span>

前提要引入 jquery和jquery.cookie jqueryform

'business_address','uploadBus','imginfo1'分別代表 圖片所在input的id form的id 和提示信息的id


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM