<input id="imgfile" type="file" name="imgfile" accept="image/*">
$(".files").on("change", function(e) {
var that = $(this);
var imgPath = that.val();
//判断上传文件的后缀名
var strExtension = imgPath.substr(imgPath.lastIndexOf('.') + 1);
if (strExtension != 'jpg' && strExtension != 'gif' &&
strExtension != 'png' && strExtension != 'bmp') {
$('body').alert('请上传图片格式文件', '确定');
return;
}
if (this.files[0].size/1024/1024>5) {
$('body').alert('图片大小请勿超过5M', '确定');
return;
}
//图片预览
var src = window.URL.createObjectURL(this.files[0]);
$(this).siblings('.addPic').empty().append('<img src="' + src + '" style="height:100%">');
var file = that[0].files;
var formData = new FormData();
formData.append('file', file[0]);
$.ajax({
type: "POST",
url: "/app/upload",
processData: false,
cache: false,
contentType: false,
data: formData,
dataType: "json",
success: function(res) {
console.log(res);
if (res.error == 0) {
resData.waybill_pic = res.img.id;
} else {
$('body').alert('图片上传失败请重新上传', '知道了');
}
},
error: function(res) {
console.log(res);
$('body').alert('图片上传失败请重新上传', '知道了');
}
});
});