Created
August 24, 2016 18:27
-
-
Save renatomattos2912/3b26551000552f8d260eab876634b6d1 to your computer and use it in GitHub Desktop.
Working with File in browser and transform base64 back to File Object
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
var blob = dataURItoBlob(res); | |
var fd = new FormData(document.forms[0]); | |
fd.append("canvasImage", blob); | |
var file = dataURLtoFile(res, 'a.jpeg'); | |
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'}); | |
} | |
function dataURLtoFile(dataurl, filename) { | |
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 File([u8arr], filename, {type:mime}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment