masnion
masnion

Reputation: 45

how to clone input type file content?

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>

Upvotes: 0

Views: 259

Answers (1)

Kiran Shahi
Kiran Shahi

Reputation: 7980

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>

Upvotes: 1

Related Questions