图片转base64

67 阅读1分钟
<!DOCTYPE html>
<html lang="">

<head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width,initial-scale=1.0" />
  <title>img - base64</title>
</head>

<body>
  <input type="file" id="imgFile" method="post" enctype="multipart/form-data">
  <button type="button" onclick="toBase64()">img-base64</button>
</body>
<script type="text/javascript">
  function toBase64() {
    const img = document.getElementById("imgFile");
    const imgFile = new FileReader();
    imgFile.readAsDataURL(img.files[0]);
    imgFile.onload = function () {
      const imgData = this.result;
      console.log(imgData);
    }
  }
</script>

</html>