首先需要引入jquery.form.js
html:
<img class="cardPostImg" src='' alt='' id='card_oppoImg' />
<form id="oppoForm" method="post">
<input class="authSubmit_input" name="idCard_oppo" id="idCard_oppoFile" onchange="card_oppoImgFun(this);show('card_oppoImg','idCard_oppoFile');" type="file" />
</form>
js:
function card_oppoImgFun(imgFile) {
var maxSize = 1024 * 1024 * 5; //最大上传图片5M
var extension = imgFile.value.substring(imgFile.value.lastIndexOf("."), imgFile.value.length); //扩展名
extension = extension.toLowerCase(); //把文件扩展名转换为小写
if((extension != '.jpg') && (extension != '.jpeg') && (extension != '.png')) {
alert("对不起,系统仅支持标准格式的照片,请您调整格式后重新上传,谢谢 !");
return
}
var file = imgFile.files[0]; //文件对象
var name = file.name; //图片名
var size = file.size; //图片大小
// if(size > maxSize) {
// $("#alipapyErweimaError").html("图片大于5M,请压缩后重新上传");
// $('#alipapyErweimaError').show();
// return false;
// }
var url = baseUrl + '/upload/singleIdImage';
$("#oppoForm").ajaxSubmit({
dataType: "json",
method: "POST",
url: url,
// data:{
// testMode:1;
// },
beforeSend: function(request) {
request.setRequestHeader("token", token);
},
success: function(data) {
if(data.code == 1) {
$('#card_oppoImg').attr('src', data.resultMap.url);
}
},
error: function(e) {
myalert("上传失败", "", "友情提示", "确定");
}
});
}