美文网首页
jQuery异步上传文件

jQuery异步上传文件

作者: Mracale | 来源:发表于2021-06-07 09:50 被阅读0次

html代码

<form method="post" action="" name="myform" id="myform" class="form-horizontal p_10">                                                              
       <input type="file" id="uploadfile"  onchange="previewcode(this)" class="input upLoadBtn" accept="*">
</form>

JS代码

//图片上传
var uimg='';
function previewcode(files) {
    $.each(files.files,function(index,file){
        if(index>1){
            console.log("最后上传一个");
            return false;
        }
                //类型判断
        if(!/^image\/(jpg|jpeg|png)$/.test(file.type)) {
            // promtTip('<p>图片类型必须是jpeg,jpg,png中的一种</p>');
            // return false;
        }
        console.log("文件类型",file);
        upFile(file);
    });
}
function upFile(file){
    var formData=new FormData();
    formData.append('Filedata',file);//加载图片文件
    $.ajax({
        url:'',
        type:"post",
        data:formData,
        //十分重要,不能省略
        cache: false,
        processData: false,
        contentType: false,
        success: function (res) {
            var info = JSON.parse(res);
            console.log(info);
        }
    });
}


相关文章

网友评论

      本文标题:jQuery异步上传文件

      本文链接:https://www.haomeiwen.com/subject/krzxeltx.html