I'm trying to convert a canvas to a Jpeg image and put it into a hidden field of a form, and then submit the form.
function createBlob() {
var imageblob = canvas.toDataURL('image/jpeg', 0.5);
document.getElementById("id_hidden_preview_field").value = imageblob; // Here we put the imageurl blob into the hidden_preview_field.
// Here we submit the form with the
$("#design").submit();
alert("after submit");
}
Here is the code of the form:
<div class="container">
<h2>Add a design</h2>
<form id="design" enctype="multipart/form-data" method="post"><input type="hidden" name="csrfmiddlewaretoken" value="qrGJSSQADxYItnN0TKUUPJA3JExfaFaP">
<input id="id_hidden_preview_field" name="hidden_preview_field" type="hidden"></p>
<!--<input type='submit' value='Save' />-->
<button id="gif" onclick="createBlob()">Save</button>
</form></div>
For some reason, when I don't put the alert (alert("after submit" + new Date().getTime());), the form gets sent without the image inside of the hidden field.
And when I DO PUT the alert, it gets sent without problems.
It makes me think that there is an issue with the form submit that destroys one of the DOM elements that it needs to send.
Does anyone now if it's really asynchronous, and if it's not that, how can I make sure that $("#design).submit(); gets called only AFTER the image gets copied into the hidden field, or maybe WITHOUT destroying the DOM elements.
Thanks a lot!