Using blob directly and removed useless code

This commit is contained in:
Mauro Mugnaini 2021-02-05 19:57:29 +01:00
parent b279221b07
commit 2279169018
1 changed files with 4 additions and 24 deletions

View File

@ -98,7 +98,6 @@
<script>
function sendAvatar() {
var dataurl = null;
var filesToUpload = avatar.files;
var file = filesToUpload[0];
@ -136,18 +135,20 @@
ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0, width, height);
dataurl = canvas.toDataURL("image/png");
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", dataURItoBlob(dataurl));
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);
@ -156,27 +157,6 @@
}
// Load files into file reader
reader.readAsDataURL(file);
return false;
}
function dataURItoBlob(dataURI) {
// convert base64/URLEncoded data component to raw binary data held in a string
var byteString;
if (dataURI.split(',')[0].indexOf('base64') >= 0) {
byteString = atob(dataURI.split(',')[1]);
} else {
byteString = unescape(dataURI.split(',')[1]);
}
// separate out the mime component
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
// write the bytes of the string to a typed array
var ia = new Uint8Array(byteString.length);
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
return new Blob([ia], {type:mimeString});
}
</script>