码迷,mamicode.com
首页 > 其他好文 > 详细

jq获取图片并换换为base64

时间:2018-12-21 19:34:53      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:func   OLE   box   targe   ima   put   fun   data   hang   

html代码:

<input type="file" id="file1"/>

jq代码:

$(‘#file1‘).change(function(e){
  var imgBox = e.target;
  uploadImg($(‘.file1‘), imgBox)
});
function uploadImg(element, tag ) {
  var file = tag.files[0];
  var imgSrc;
  if (!/image\/\w+/.test(file.type)) {
  alert("请上传图片!");
  return false;
  }
  var reader = new FileReader();
  reader.readAsDataURL(file);
  reader.onload = function() {
  //console.log(this.result);
  imgSrc = this.result;
  $(element).attr("src", imgSrc);
  };
}

jq获取图片并换换为base64

标签:func   OLE   box   targe   ima   put   fun   data   hang   

原文地址:https://www.cnblogs.com/zdzdbk/p/10158445.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!