screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <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 src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script> </head> <body> <div id="app"></div> <script type="text/babel"> const { useState } = React; function App() { const handleFileChange = (event) => { const file = event.target.files[0]; // File type validation if (!file.type.startsWith('image/')) { alert('Please upload only image files.'); return; } // File size validation (in bytes) const maxSize = 5 * 1024 ; if (file.size > maxSize) { alert('File size exceeds the limit (5 KB).'); return; } }; return ( <div className="container"> <h3 className="title">Image Upload Size Limit and File Type Restriction in React JS</h3> <input type="file" onChange={handleFileChange} /> </div> ); } ReactDOM.render(<App />, document.getElementById("app")); </script> <style> * { box-sizing: border-box; } .container { margin: 0 auto; width: 600px; text-align: center; box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.12), 0 2px 4px 0 rgba(0, 0, 0, 0.24); padding: 20px; } .title { font-size: 24px; color: #333; margin-bottom: 20px; } .uploaded-text { font-size: 18px; color: #333; margin-top: 20px; } .uploaded-image { max-width: 100%; height: auto; margin-top: 10px; border-radius: 5px; box-shadow: 0 0 5px rgba(0, 0, 0, 0.2); } .error { color: #ff0000; font-size: 14px; margin-top: 10px; } .image-size { font-size: 16px; color: #777; margin-top: 10px; } </style> </body> </html>