How to upload this to the PHP server using jQuery?
It’s quite simple: Convert your canvas to blob
and send an Ajax, with a small modification in the settings.
function ajaxUpload(options) {
options = $.extend({}, {contentType: false, cache: false, processData: false}, options);
return $.ajax(options);
}
canvas.toBlob(function (blob) {
var data = new FormData();
data.append('imagem', blob);
ajaxUpload({url: '/upload.php', data: data}).success(function () {
console.log('upload concluído com sucesso');
});
});
Is it the right way to upload? It won’t harm me because it’s not a normal upload, with form input etc..?
Well, the only difference I see is that you will always have to use ajax. Besides, with input
, usually in the upload submission, the file name is sent, different from the blob
, that does not have the name of the source file, but that in the case could be solved in a simple way: Placing the third parameter in the call of .append
:
data.append('imagem', blob, 'imagem.jpg')
Moreover, there are no "problems", but differences.
Need: does not lock the page, because the blob will be built on 2° plane. Otherwise I would say that this callback is useless.
– Klaider
#toBlob
probably duplicates the canvas data, while#toDataURL
does a lot more. If callback is pq has obviously overhead, zzzzz. I’m not saying it’s worse to blobar than to base64ear at any time, dear.– Klaider