Created
December 9, 2012 20:26
-
-
Save kosso/4246840 to your computer and use it in GitHub Desktop.
convert base64 to raw binary data held in a string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
via http://stackoverflow.com/questions/4998908/convert-data-uri-to-file-then-append-to-formdata/5100158 | |
via http://www.smartjava.org/content/face-detection-using-html5-javascript-webrtc-websockets-jetty-and-javacvopencv | |
**/ | |
function dataURItoBlob(dataURI) { | |
var binary = atob(dataURI.split(',')[1]); | |
var array = []; | |
for(var i = 0; i < binary.length; i++) { | |
array.push(binary.charCodeAt(i)); | |
} | |
return new Blob([new Uint8Array(array)], {type: 'image/jpeg'}); | |
} | |
/// | |
From there, appending the data to a form such that it will be uploaded as a file is easy: | |
var dataURL = canvas.toDataURL('image/jpeg', 0.5); | |
var blob = dataURItoBlob(dataURL); | |
var fd = new FormData(document.forms[0]); | |
fd.append("canvasImage", blob); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment