You can find the working file at Jsfiddle
/* JavaScript */
function readURL() {
var $input = $(this);
if (this.files && this.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$input.next('.blah').attr('src', e.target.result).show();
$input.after('<input type="button" class="delbtn" value="remove">');
}
reader.readAsDataURL(this.files[0]);
}
}
$(".imgInp").change(readURL);
$("form").on('click', '.delbtn', function(e) {
var $input = $(this);
$input.next('.blah').attr('src', e.target.result).hide();
$input.prev('.imgInp').val("");
$(this).closest(".delbtn").remove();
});
<form name="" action="" method="post">
<div class="div">
<input type='file' class="imgInp blah" />
<img class="blah" src="#" alt="your image"/></div>
<br>
<br>
<div class="div">
<input type='file' class="imgInp" />
<img class="blah" src="#" alt="your image"/></div>
</form>
Display and remove image is working fine.
But if the image already selected and again select another new image then preview function is not working. Also remove button keep adding. Please check the error image below.
Click to view the error
I have updated your fiddle at following link,
https://jsfiddle.net/balasuar/97dzkf70/20/
When first time you select the image, the next element of the button is the image. But once the image selected, you added theRemove button element next to the file control. So the next time, it breaks as the image is no longer next to the file control.
I fixed it below by having a reset method to clear this.
function readURL() {
var $input = $(this);
if (this.files && this.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
reset($input.next('.delbtn'), true);
$input.next('.blah').attr('src', e.target.result).show();
$input.after('<input type="button" class="delbtn" value="remove">');
}
reader.readAsDataURL(this.files[0]);
}
}
$(".imgInp").change(readURL);
$("form").on('click', '.delbtn', function(e) {
reset($(this));
});
function reset(elm, prserveFileName){
if(elm && elm.length > 0) {
var $input = elm;
$input.next('.blah').attr('src', '').hide();
if(!prserveFileName){
$input.prev('.imgInp').val("");
}
elm.remove();
}
}
Related
I'm trying to make an image uploader, and after the upload, make it movable and resizable. I have 2 scripts with jquery, I can upload an image, and move/resize an other one, but can't use it together.
uploader:
<div class="upload">
<input type='file' onchange="readURL(this);" /> <br>
</div>
<div class="block">
<div id="background">
<img id="bg" src="" alt="" width="auto" height="50%"/>
</div>
</div>
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#bg')
.attr('src', e.target.result);
};
reader.readAsDataURL(input.files[0]);
}
}
make another image resizable and movable:
$(document).ready(function () {
$(document).on('click', '.block-add', function () {
var a = $(this);
var src = a.find('img:first').attr('src');
var elem = $('<div class="container"><img src="' + src + '" class="blocks" /></div>');
$('.block').append(elem);
elem.draggable();
elem.find('.blocks:first').resizable();
return false;
});
});
It works separately but if I want to make it to an uploaded image, the image just doesn't appear.
the operation you wanna do is a little bit confusing.
first of all, decide what you are gonna do? If it was me I would use libraries like Comman js or Pica resize image lib for production use in order to store it on back-end too.
How can I display an image I uploaded immediately in HTML using button like a profile picture
Thank you
<div>
<img id="myImg" class="img img-circle" src="#" alt="your image"
height=200 width=100>
</div>
<input type='file' />
$(function () {
$(":file").change(function () {
if (this.files && this.files[0]) {
var reader = new FileReader();
reader.onload = imageIsLoaded;
reader.readAsDataURL(this.files[0]);
}
});
});
function imageIsLoaded(e) {
$('#myImg').attr('src', e.target.result);
$('#yourImage').attr('src', e.target.result);
};
So you essentially want an image preview without a page reload? Here is some code that will do just that.
HTML:
<img id="myImg" alt="your image" class="img img-circle" style="visibility: hidden; display: none" height="200" width="100">
<input type="file" onchange="showimagepreview(this)" accept=".png,.jpg,.jpeg,.gif" />
JavaScript:
function showimagepreview(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
document.getElementById("myImg").setAttribute("src", e.target.result);
document.getElementById("myImg").style = "visibility: visible; display: inline";
}
reader.readAsDataURL(input.files[0]);
}
}
Your code already successfully displays the image preview. As far as I understand your question you intend to display the full image on button click.
You will need an image for that
<img id="yourImage">
on file upload you will need to store the src somewhere, for the sake of simplicity that will be a global variable here:
var src = "";
and then:
function imageIsLoaded(e) {
$('#myImg').attr('src', e.target.result);
src = e.target.result;
};
and the button click:
$("#yourbutton").click(function() {
$("#yourImage").att("src", src);
});
So basically you want to use an image as a button.Here is the solution you are looking for
<button>
<img src="http://www.w3.org/html/logo/downloads/HTML5_Logo_32.png" id="dagger" />
</button>
$(function(){
$("#dagger").click(function(){
alert("click");
});
});
Trying to upload an image file with preview using Jquery. It's working and I got the preview, but when I click on the upload button I saw an error message
Uncaught RangeError: Maximum call stack size exceeded
in the console.
Here is the code:
$(document).ready(function() {
var readURL = function(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$('.profile-image').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$(".file-upload").on('change', function() {
readURL(this);
});
$(".upload-button").on('click', function() {
$(".file-upload").click();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<img class="profile-image" src="demo.png" alt="Profile Image">
<a class="profile-img-edit upload-button">
<i class="fas fa-camera"></i> Select Image
<input class="file-upload" type="file" name="file" size="40" accept=".png, .jpg, .jpeg, .gif">
</a>
</div>
The issue is because the click event on the .file-upload element bubbles up to .upload-button and is caught by the click handler, which triggers a click handler on .file-upload which bubbles up to .upload-button which triggers a click... and so on.
To fix this, stop the event from bubbling up from the .file-upload by adding a click event handler to it and calling stopPropagation() on the event, like this:
$(document).ready(function() {
var readURL = function(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$('.profile-image').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$(".file-upload").on('change click', function(e) {
e.stopPropagation();
readURL(this);
});
$(".upload-button").on('click', function() {
$(".file-upload").click();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<img class="profile-image" src="demo.png" alt="Profile Image">
<a class="profile-img-edit upload-button">
<i class="fas fa-camera"></i> Select Image
<input class="file-upload" type="file" name="file" size="40" accept=".png, .jpg, .jpeg, .gif">
</a>
</div>
I've tried several different ways to upload image to database, using Bootstrap. My IDE is Visual Studio. What I need is to let the uploaded image appear in a preview. Although I've seen a working demo before, when I run my code, the photo file appears on screen without a preview image. Because of this, I'm not sure if the image is stored in database successfully or not. Any advice would be very appreciated! I've included all of the current code:
HTML:
<div>
<form>
<div>
<div class="form-group hirehide is-empty is-fileinput width100">
<div class=socialmediaside2>
<input class=fileUpload accept="image/jpeg, image/jpg" name=profilepic[] type=file value="Choose a file">
<div class=input-group>
<input class=form-control id=uploadre placeholder="Please select your profile picture" readonly>
<span class="input-group-btn input-group-sm">
<button class="btn btn-fab btn-fab-mini"type=button><i class=material-icons>attach_file</i></button>
</span>
</div>
</div>
</div>
<div class=upload-demo>
<div class=upload-demo-wrap><img alt="your image" class=portimg src=#></div>
</div>
</div>
</form>
</div>
JavaScript:
function readURL() {
var $input = $(this);
var $newinput = $(this).parent().parent().parent().find('.portimg');
if (this.files && this.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
reset($newinput.next('.delbtn'), true);
$newinput.attr('src', e.target.result).show();
$newinput.after('<input type="button" class="delbtn removebtn" value="remove">');
}
reader.readAsDataURL(this.files[0]);
}
}
$(".fileUpload").change(readURL);
$("form").on('click', '.delbtn', function (e) {
reset($(this));
});
function reset(elm, prserveFileName) {
if (elm && elm.length > 0) {
var $input = elm;
$input.prev('.portimg').attr('src', '').hide();
if (!prserveFileName) {
$($input).parent().parent().parent().find('input.fileUpload').val("");
//input.fileUpload and input#uploadre both need to empty values for particular div
}
elm.remove();
}
}
CSS:
body {
img.portimg {
display: none;
max-width: 200px;
max-height: 200px;
}
}
I am using javascript code.this code show the image which image you select before uploading.
Javascript
<script>
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#blah').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$("#imgInp").change(function(){
readURL(this);
});
</script>
Html
<form id="imageform" method="post" enctype="multipart/form-data" >
<input type="file" name="photoimg" id="imgInp" >
<img id="blah" src="#" alt="your image" />
<input type="submit" Value="Upload">
</form>
This piece of code work perfectly.SO i need normaly when image not select than image dispaly:none; and if image select than show image.i want to handle this problem with client side scripting.
Modify the markup so that the image is hidden by default:
<img id="blah" src="#" alt="your image" style="display:none" />
And then call the jQuery show method on the image when you have read the file like so:
$('#blah').attr('src', e.target.result);
$('#blah').show();
Fiddle is here
Try
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#blah').prop('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
jQuery(function () {
$("#imgInp").change(function () {
readURL(this);
});
})
Demo: Fiddle