Skip to content

Instantly share code, notes, and snippets.

@kosso
Created December 9, 2012 20:26
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save kosso/4246840 to your computer and use it in GitHub Desktop.
Save kosso/4246840 to your computer and use it in GitHub Desktop.
convert base64 to raw binary data held in a string
/**
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