base64.js
jquery plugin for converting a file to base64 string
A simple plugin for converting any file to base64 string in browser which is File Reader API compatible. Easy to understand,modify and use it in your project.
How to use
Attach the latest JQuery.js/jQuery.min.js and the plugin.js provided.
It is very easy to implement the file converter, Just create an input tag and call the base64 merhod with it's ID.
<input id="fileBrowser"/> <script> $("#fileBrowser").base64(); </script>
Options
- customFileBrowser
- inputStyle
- buttonRequired
- buttonStyle
- buttonText
- acceptableFormats
- minSize
- maxSize
- onError
- onSuccess
Boolean, default is true. This option specifies whether a custom Input element is required or not.
String, if the custome file browser provided by the plugin is used then Dev can specify the style(CSS) for that element. Styles are not maintained in a different CSS file since there is very few styles we have used. If required the a class can be added to the HTML which gets injected through the script and add your CSS in your own stylesheets.
Boolean,default is true. If the custom file browser is used then a button comes along with it.If it is not required for your project then you can only use the input element without the button by passing 'false'.
String, If the custome button is used then Dev has the option to change the look of the button by passing the styles through this attribute.
String, If the custome button is used then Dev has the option to change the text of the button by passing a string through this attribute.
an Array,this attribute accepts the format of the file which the Dev wants the user to select and then the base64.js to convert it to base64 string.
http://www.freeformatter.com/mime-types-list.html
Number/String as number, A validation is performed for the minimum size of the file which the user can upload. If the validation is failed then the method passsed through the 'onError' attribute will be called.
Number/String as number, A validation is performed for the minimum size of the file which the user can upload. If the validation is failed then the method passsed through the 'onError' attribute will be called.
javascript function, a method which could be called when an error or exception occurs. The DOM object,Error code and the Error description are passed as the parameters to the method.
javascript function, a method which is called when the file conversion is success. The DOM object and the base64 string is passed as the parameters to the method.
Example
$("#fileBrowser").base64({ "customFileBrowser":true, "inputStyle":"", "buttonRequired": true, "buttonStyle":"", "buttonText":"Choose a file", "acceptableFormats":["text/html"], "minSize":"",//in bytes "maxSize":"",//in bytes "minSizeErrorMessage":"File size is less than minimum expected size.", "maxSizeErrorMessage":"File size is more than maximum expected size.", "formatErrorMessage":"Selected file doesn't match with the specified formats.", "onError":function(inst,errorCode,errMsg){ console.log(errMsg); }, "onSuccess":function(inst,base64Str){ console.log("success"); } });