How to get toDataURL to save in a sessionStorage?

Asked

Viewed 135 times

0

I’m using a library I picked up here at this link: https://github.com/fengyuanchen/cropper

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

If anyone can give me a hand... Thank you

1 answer

2


You can try it this way:

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 specific times:

When 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.

  • More I have no idea where to add this excerpt...

  • When you want to do it?

  • When to cut the image

  • Check the edition.

  • I put it like this:

  • 'cropend.Cropper': Function(e) { var canvas = $(). Cropper('getCroppedCanvas'); //grabs the canvas var data = canvas.toDataURL('image/jpeg'); //generates Base64 with mimetype var Base64 = data.split(','). Reverse()[0]; //takes only the code Alert(Base64); //shows in Storage }

  • more in google Chrome shows Uncaught error Typeerror: canvas.toDataURL is not a Function

  • Guys I already solved the problem. kkkk ...

  • to clarify, I put more detailed the codes within the events.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.