base64.html 1.3 KB

1234567891011121314151617181920212223242526272829
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="description" content="在线Base64生成转换小工具,可以实现任意文件转Base64 Data-URI编码,文件往页面中一拖即可。" />
  6. <meta name="keywords" content="base64, FileReader, readAsDataURL, 文件" />
  7. <meta name="author" content="menglu" />
  8. <title>任意文件转base64</title>
  9. <style>
  10. body { word-break: break-all; margin: 0 1em; min-height: 100vh; font-family: Consolas, "Andale Mono", "Lucida Console", "Lucida Sans Typewriter", Monaco, "Courier New", monospace; overflow: hidden;}
  11. .empty::before{position: absolute; font-size: 50px; content: '任意文件\A拖到这里'; white-space: pre; left: 50%; top: 50%; transform: translate(-50%,-50%); color: gray;}
  12. </style>
  13. </head>
  14. <body class="empty">
  15. <script>
  16. window.addEventListener("dragenter", function(event) { event.preventDefault(); }, false);
  17. window.addEventListener("dragover", function(event) { event.preventDefault(); }, false);
  18. window.addEventListener("drop", function(event) {
  19. var reader = new FileReader();
  20. reader.onload = function(e) {
  21. document.body.insertAdjacentHTML("afterBegin", '<p>' + e.target.result + '</p>');
  22. document.body.classList.remove('empty');
  23. };
  24. reader.readAsDataURL(event.dataTransfer.files[0]);
  25. event.preventDefault();
  26. }, false);
  27. </script>
  28. </body>
  29. </html>