screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script> </head> <body> <div id="app"></div> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.development.js"></script> <script type="text/babel"> const { useState } = React; function App() { const [imageData, setImageData] = useState({ base64textString: '', imageName: '', showImage: false, }); const convertToBase64 = (event) => { const file = event.target.files[0]; const reader = new FileReader(); reader.readAsDataURL(file); reader.onload = () => { setImageData({ base64textString: reader.result, imageName: file.name, showImage: true, }); }; reader.onerror = (error) => { console.log('Error: ', error); }; }; return ( <div className='container'> <h3>React Convert Image to Base64</h3> <input type="file" onChange={convertToBase64} accept="image/*" /> <br /> {imageData.showImage && ( <div> <img src={imageData.base64textString} alt={imageData.imageName} /> </div> )} <br /> <textarea value={imageData.base64textString} onChange={(e) => setImageData({ ...imageData, base64textString: e.target.value, }) } rows="5" ></textarea> </div> ); } ReactDOM.render(<App />, document.getElementById('app')); </script> <style scoped> .container { max-width: 600px; margin: 0 auto; padding: 20px; border-radius: 5px; box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.2); } h3 { text-align: center; margin-top: 0; } input[type="file"] { margin-bottom: 10px; } textarea { width: 100%; padding: 10px; font-size: 16px; border: 1px solid #ccc; border-radius: 5px; resize: none; box-sizing: border-box; } img { max-width: 100%; height: auto; } </style> </body> </html>