图片 base64 编码 转 blob
// 图片 base64 url 转 blob
function dataURLtoBlob(dataurl) {
var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new Blob([u8arr], { type: mime });
}
监听 change 事件 并使用 ajax 传到 后端
// 选择 图片
$('input[type="file"]').change(function(){
// console.log(this.files);
if (!this.files[0].type.match('image.*')) {
return false;
}
var reader = new FileReader();
var _this = this;
reader.onload = function(){
$(_this).parent().find('img').prop('src', event.target.result);
v