0

I have <canvas> and I get result by base64 .

canvas.toDataURL('image/png');

I want send to server but don't send base64 . I want send file PNG. Is it possible?

Thank you JoeJoe87577 / work example:

 var dataurl ='data:image/png;base64,i........I='
    function dataURLtoBlob(dataurl) {
        var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
            bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
        while(n--){
            u8arr[n] = bstr.charCodeAt(n);
        }
        return new Blob([u8arr], {type:mime});
    }

    //var dataurl = canvas.toDataURL()
    var blob = dataURLtoBlob(dataurl);
    var fd = new FormData();
    fd.append("key", "6528448c258cff474ca9701c5bab6927");
    fd.append("file", blob);
    var xhr = new XMLHttpRequest();
    xhr.open('POST', 'http:/', true);
    xhr.onload = function(){
        //alert('upload complete');
    };
    xhr.send(fd);
2
  • 1
    Have a look at this. With the information provided you'll get the binary data from the base64 string. Then you can send the data via ajax or an XMLHttpRequest. Commented Feb 12, 2016 at 13:34
  • make pease answer and i want accept him Commented Mar 19, 2016 at 10:17

2 Answers 2

1

Yes, you can send it as ajax post request after converting an image to base64 string, check this demo.

Then you can make an ajax post request

xhttp.open("POST", "ajax_test.asp", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send( imageBase64Data );
Sign up to request clarification or add additional context in comments.

Comments

0

Use canvas.getImageData()

Also see the MDN Documentation for CanvasRenderingContext2D.getImageData().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.