Skip to content

Instantly share code, notes, and snippets.

@kirasiris
Last active February 14, 2018 22:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirasiris/d479a09cc0cc56510a375bd7191edf1e to your computer and use it in GitHub Desktop.
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)
<!-- 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>
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