js-img2base64
Converts an image to Base64. You could pass a maximum size of an image in megabytes, and an array of valid extensions. This package has no external dependency packages.
Install
npm install js-img2base64 --save
Usage
var imageToBase64 = ;
Or
;
API
imageToBase64(imageFile, maxSizeInMegabytes, validExtensions)
Convert image file to image base64 data
Arguments
- {Image}
imageFile
, required - {number}
maxSizeInMegabytes
optional, default <= 3 megabytes - {array}
validExtensions
optional, default ['png', 'jpeg', 'jpg', 'gif']
Return type
- return type: Promise. Resolved if everything is okay, returns the Base64 string of the file. Rejected if any validation fails (maxSizeInMegabytes, validExtensions or a FileReader error), returns the rejection reason (error).
;
Usage example with React
;; const ImageComponent = { const handleChange = { const value = eventtargetfiles0; ; } return <input accept="image/*" onChange=handleChange id="button-file" type="file" /> ;};