Skip to content

Instantly share code, notes, and snippets.

@ronykader
Created February 14, 2017 12:26
Show Gist options
  • Save ronykader/7bc4934bca14c91f1feb78e37a71d664 to your computer and use it in GitHub Desktop.
Save ronykader/7bc4934bca14c91f1feb78e37a71d664 to your computer and use it in GitHub Desktop.
Image Preview function by javaScript when we upload image by php
Html Code
----------------------------------
<div class="showpreloadimage">
<img id="showimageurl1" width="100%">
</div>
<input type="file" class="filestyle" name="team_image" data-placeholder="No file" id="postimage1" onchange="upload_teamimage(this)" tabindex="-1" style="position: absolute; clip: rect(0px 0px 0px 0px);">
<div class="bootstrap-filestyle input-group">
<input type="text" class="form-control " placeholder="No file" disabled="">
<span class="group-span-filestyle input-group-btn" tabindex="0">
<label for="postimage1" class="btn btn-default ">
<span class="icon-span-filestyle glyphicon glyphicon-folder-open"></span>
<span class="buttonText">Choose file</span>
</label>
</span>
</div>
javaScript Code
-------------------------------
function upload_teamimage(postimage,target)
{
var fr = new FileReader();
fr.onload = function(e) { target.src = this.result; };
postimage.addEventListener("change",function() {
fr.readAsDataURL(upload_src.files[0]);
});
}
var upload_src = document.getElementById("postimage1");
var showimage_url = document.getElementById("showimageurl1");
upload_teamimage(upload_src,showimage_url);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment