JS获取图片base64值

前端开发   发布日期:2023年05月03日   浏览次数:464
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <meta charset="gbk">
  5.         <title></title>
  6.     </head>
  7.     <body>
  8.         <input accept="image/*" name="upimage" id="upload_file" type="file" onchange="gen_base64()">
  9.      <br/>
  10.         <textarea id="base64_output" name="Word" style=" width:820px"></textarea>
  11.      <br/>
  12.      <img src="" id="myImg" />
  13.         <script type="text/javascript">
  14.             function $_(id) {
  15.                     return document.getElementById(id);
  16.             }
  17.             function gen_base64() {
  18.                 var file = $_('upload_file').files[0];
  19.                 r = new FileReader();  //本地预览
  20.                 r.onload = function(){
  21.                     $_('base64_output').value = r.result;
  22.              $_('myImg').src= r.result;
  23.                 }
  24.                 r.readAsDataURL(file);    //Base64
  25.             }
  26.         </script>
  27.     </body>
  28. </html>


以上就是JS获取图片base64值的详细内容,更多关于JS获取图片base64值的资料请关注九品源码其它相关文章!