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

转载于:https://www.cnblogs.com/womow/p/4514191.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值