0

I have this input typed file, when a user uploads a photo(not submit), it clones this photo and appends it to the div

<input type="file" class="upload"></input>
<button class="submit"></button
<div class="append-here" style="width:500px, height: 500px; background:red;></div>


<script>
var y = $(".append-here")
$(".upload").val().clone(true, true).appendTo(y);
</script>
2

1 Answer 1

1

On your input change use the FileReader object and read your input file property:

$('.submit').on('click', function() {
    $(this).hide();
    var preview = $('img');
    preview.show();
    var file = $('input[type=file]').prop('files')[0];
    var reader = new FileReader();
    reader.addEventListener("load", function() {
        preview.attr('src', reader.result);
    }, false);
    if (file) {
        reader.readAsDataURL(file);
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="file" class="upload"/>
<img src="" height="200" alt="Image preview..." style="display:none;"/>
<button class="submit">Submit</button>

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your answer but there is just one thing .. I want the preview to be done when i click the submit button and thanks
WORKS LIKE A CHARM :>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.