How do I get toDataURL to save in a sessionStorage?

0

I'm using a library I got here at this link: link

I've already spent 2 days testing and looking for how to get the image cut with toDataURL to get the base64 and save it in a sessionStorage for me to use later.

If someone can lend a hand ... Thanks

    
asked by anonymous 16.10.2016 / 20:00

1 answer

2

You can try this:

var canvas = $(img_selector).cropper('getCroppedCanvas'); //pega o canvas
var data = canvas.toDataURL('image/jpeg'); //gera a base64 com o mimetype
var base64 = data.split(',').reverse()[0]; //pega somente o código
sessionStorage.setItem("base64", base64); //salva na storage

You can call this piece of code at a few specific times:

When the crop is finished:

$(img_selector).on('cropend.cropper', function (e) {
    var $cropper = $(e.target);
    var canvas = $cropper.cropper('getCroppedCanvas');
    [...]
    // cole o resto do código aqui
});

When the cropbox is changing:

$(img_selector).on('cropmove.cropper', function (e) {
    var $cropper = $(e.target);
    var canvas = $cropper.cropper('getCroppedCanvas');
    [...]
    // cole o resto do código aqui
});

Other plugin events can be found here .

    
16.10.2016 / 20:44