Skip to content

Instantly share code, notes, and snippets.

@alash3al
Last active August 29, 2015 14:15
Show Gist options
  • Save alash3al/368bab5eedccefaab365 to your computer and use it in GitHub Desktop.
Save alash3al/368bab5eedccefaab365 to your computer and use it in GitHub Desktop.
javascript file2base46, return an uploaded file as base64 encoded
/**
* 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