Last active
December 25, 2019 01:45
-
-
Save vihoangson/8f6c3e92516072cc83426c4298794344 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Vào một ngày đẹp trời tôi có nhã hứng muốn chọn file và hiện thị preview lên giao diện ngay lập tức. Sau đây là code thực hiện. | |
<img> | |
<input type="file"> | |
<script | |
src="https://code.jquery.com/jquery-3.4.1.js" | |
integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" | |
crossorigin="anonymous"></script> | |
<script> | |
$("input[type=file]").on('change', function () { | |
var preview = document.querySelector('img'); // Image reference | |
var file = document.querySelector('input[type=file]').files[0]; // File refrence | |
var reader = new FileReader(); // Creating reader instance from FileReader() API | |
reader.addEventListener("load", function () { // Setting up base64 URL on image | |
preview.src = reader.result; | |
}, false); | |
reader.readAsDataURL(file); | |
}) | |
</script> |
Author
vihoangson
commented
Dec 25, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment