base64上传图片
<div class='img_box' id="img_box">
<img id="LogoImg" src="../../images/moren.png" onerror='this.src="../../images/moren.png"' data-preview-src="" data-preview-group="1" class="not-preview-auto">
<div class="img_tips">点击修改图片
<input type="file" id="xdaTanFileImg" onchange="btnUpload(event)" accept="image/*" capture="camera" style="width:125px;position:absolute;left: 0;top: 0;bottom: 0;opacity: 0;">
</div>
</div>
function btnUpload(event) {
var reader = new FileReader();
var AllowImgFileSize = 2 * 1024 * 1024;
var file = $("#xdaTanFileImg")[0].files[0];
var imgUrlBase64;
var path = $("#xdaTanFileImg").val();
console.log(path);
var fileName = path.substring(path.lastIndexOf("/") + 1);
var fileType = fileName.substring(fileName.lastIndexOf("."));
var compressPath = '_doc/' + fileName.substring(0, fileName.lastIndexOf(".")) + "_compress_" + GetUUID() + fileType;
if (file) {
imgUrlBase64 = reader.readAsDataURL(file);
reader.onload = function (e) {
var dataURL = e.target.result;
var base64 = dataURL.split(",")[1];
$("#LogoImg").attr('src',e.target.result);
if (AllowImgFileSize != 0 && AllowImgFileSize < reader.result.length) {
mui.toast("文件大小不符合规范");
return;
} else{
mui.toast("图片上传失败");
}
}
}
}