convertto-base64
v1.1.2
Published
Convert any type of file to base64 with these package.Keep in mind that file size shoud be less
Downloads
86
Readme
Convert file to Base64 encoded
- Install the package
- Call this function where and which file you want to convert
- Example for the
react
component - For image >
<input type="file" accept="image/*" onChange={handleImageUpload} />
- Example for the
Follow this steps
- Install & Import the package
import base64 from "convertto-base64";
- Make your input field which will take image/pdf/doc/video
<input type="file" accept="image/*" onChange={handleImageUpload} />
- Create your State & Function following this
const [image, setImage] = useState([]);
const handleImageUpload = async (e) => {
e.preventDefault();
const base64Data = await base64(e.target.files[0]);
setImage(base64Data);
// You can set it to state, local storage, or any other appropriate action.
};
- Use the
image
state anywhere you want, following this
<img src={image} height={200} width={400} alt="image">
- If you want to view the
base64
data. You can show it in theconsole.log()
or
<textarea defaultValue={image}></textarea>