Last active
August 29, 2015 14:15
-
-
Save alash3al/368bab5eedccefaab365 to your computer and use it in GitHub Desktop.
javascript file2base46, return an uploaded file as base64 encoded
This file contains hidden or 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
/** | |
* File2Base64 | |
* | |
* @param string file the path of the files | |
* @param string mim_type the type of the file e.g: 'image/png' | |
* @param callable callback the callback that handle the base64encoded string | |
* | |
* @author Mohammed Al Ashaal | |
* @version 1.0.0 | |
*/ | |
function file2base64(file, mime_type, callback) | |
{ | |
reader = new FileReader(); | |
reader.onload = function(e) | |
{ | |
data = e.target.result; | |
callback('data:'+mime_type+';base64,'+ btoa(data)); | |
}; | |
reader.readAsBinaryString(file); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment