🔔 Alert..!! Get 2 Month Free Cloud Hosting With $200 Bonus From Digital Ocean ACTIVATE DEAL

base64.js is a very light jQuery file base64 converter plugin which processes a file input and returns a base64 string using FileReader web api.

Base64

Documentation

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
  • Boolean, default is true. This option specifies whether a custom Input element is required or not.

  • inputStyle
  • 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.

  • buttonRequired
  • 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'.

  • buttonStyle
  • 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.

  • buttonText
  • 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.

  • acceptableFormats
  • 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.

    Refer the below link for all the MIME types a browser can accept
    http://www.freeformatter.com/mime-types-list.html


  • minSize
  • 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.

  • maxSize
  • 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.

  • onError
  • 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.

  • onSuccess
  • 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");  		}         });

You May Also Like