111.html 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <input type="file" onchange="previewFile()" /><br />
  11. <img src="" height="200" alt="Image preview..." />
  12. <!-- // -->
  13. <!-- -->
  14. <script>
  15. function previewFile() {
  16. var preview = document.querySelector("img");
  17. var file = document.querySelector("input[type=file]").files[0];
  18. var reader = new FileReader();
  19. console.log(preview, file, reader);
  20. reader.onloadend = function () {
  21. preview.src = reader.result;
  22. };
  23. if (file) {
  24. reader.readAsDataURL(file);
  25. } else {
  26. preview.src = "";
  27. }
  28. }
  29. </script>
  30. </body>
  31. </html>