Changing a DIV background in iframe

Asked

Viewed 161 times

0

Following staff,

I have a form to upload any image...

<form id="form-upload" enctype="multipart/form-data" action="url" method="post">
    <input type="file" name="image" id="image" />
    <input type="submit" name="salvar" value="Salvar" />
<form>

I want to put the image that was sent, as background of a div in an iframe, using ajax, not to need to update the page, I would like to know a way to do this, it is really necessary to upload the image?


My JS code:

$('#form-upload').on('submit',function (e) {
    e.preventDefault();

    var formData = new FormData(this);

    $.ajax(
    {
        url: uma_url_qualquer,
        type: 'POST',
        data: formData,
        success: function (data) {
            console.log("sucesso");
        },
        cache: false,
        contentType: false,
        processData: false
    });
});

1 answer

2


Yes it is possible and, if the image should not be saved to be displayed every time the user accesses the page, really do not need to upload...

$('#arquivo').change(function(e) {
    var _arq = URL.createObjectURL(e.target.files[0]);
    $("#frame").contents().find("#div").css('background-image','url('+_arq+')');
});

See the example working here: https://jsfiddle.net/he280vrn/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.