Last active
February 14, 2018 22:37
-
-
Save kirasiris/d479a09cc0cc56510a375bd7191edf1e to your computer and use it in GitHub Desktop.
Codigo de Javascript para hacer un preview de Imagen antes de subir a base de datos(database)
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
<!-- Div escondido que se mostrara hasta que haya un valor dentro del elemento <img> --> | |
<div class="thumbnail" id="div-preview" style="display:none;"> | |
<img alt="Image Display Here" id="image_preview" src="#" /> | |
</div> | |
<!-- Post Image --> | |
<div class="form-group"> | |
<?php echo form_label('Upload Image', 'post_image') ?> | |
<?php | |
$data = array( | |
'name' => 'post_image', | |
'id' => 'post_image', | |
'class' => 'form-control', | |
'onchange' => 'readURL(this)', | |
'type' => 'file', | |
'value' => set_value('post_image') | |
); | |
?> | |
<?php echo form_upload($data); ?> | |
</div> |
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
function readURL(input) { | |
// Esconde el div a menos que haya contenido dentro | |
var x = document.getElementById("div-preview"); | |
if (x.style.display === "none") { | |
x.style.display = "block"; | |
} | |
// Muestra la imagen si es que existe | |
if (input.files && input.files[0]) { | |
var reader = new FileReader(); | |
reader.onload = function (e) { | |
// ID de la imagen(si es que existe). | |
$('#image_preview').attr('src', e.target.result); | |
} | |
reader.readAsDataURL(input.files[0]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment