Right use of the toBlob function

This commit is contained in:
Mauro Mugnaini 2021-02-05 20:08:25 +01:00
parent 2279169018
commit aa07c719d3
1 changed files with 18 additions and 18 deletions

View File

@ -135,24 +135,24 @@
ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0, width, height);
var imageBlob = canvas.toBlob("image/png");
var form = document.getElementById('theForm')
var formData = new FormData(form)
// Deleting the big image from the form data to prevent send
formData.delete('imageSelector')
// Adding new blob with new image with right size
formData.append("image", imageBlob);
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
location.reload();
} else {
alert('An error occurred uploading avatar image')
}
};
xhr.open(form.method, form.action, true);
xhr.send(formData);
canvas.toBlob(function(blob) {
var form = document.getElementById('theForm')
var formData = new FormData(form)
// Deleting the big image from the form data to prevent send
formData.delete('imageSelector')
// Adding new blob with new image with right size
formData.append("image", blob);
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
location.reload();
} else {
alert('An error occurred uploading avatar image')
}
};
xhr.open(form.method, form.action, true);
xhr.send(formData);
});
} // img.onload
}
// Load files into file reader