【转载】将本地图片转成base64

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<input type="file" id="inp">
<img id="img">
<script>
  document.getElementById("inp").onchange = function () {
    var file = this.files[0];
    var a = new FileReader();
    a.onload = function (e) {
      console.log(e);
      var base64 = e.target.result;
      document.getElementById("img").src = base64
    }
    a.error = function () {
      console.log("失败");
    }
    a.readAsDataURL(file);
  }
</script>
</body>
</html>
posted @ 2019-12-24 19:04  jser_dimple  阅读(615)  评论(0编辑  收藏  举报
/*function gotourl() { var url = "https://www.cnblogs.com/smart-girl/"; var localurl = document.url; if( localurl.substring(0,url.length) != url ) { location.href=url; } } gotourl();*/