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 MAX_FILE_SIZE_MB = 5; const [errorMessage, setErrorMessage] = useState(''); const isValidFileType = (file) => { return file.name.endsWith('.csv'); }; const isValidFileSize = (file) => { return file.size <= MAX_FILE_SIZE_MB * 1024 * 1024; }; const handleFileChange = (event) => { const selectedFile = event.target.files[0]; if (selectedFile) { if (isValidFileType(selectedFile) && isValidFileSize(selectedFile)) { setErrorMessage(''); // Clear any previous error message } else { setErrorMessage('Invalid file. Please check file type and size.'); } } }; return ( <div className="container"> <h1>React Js Validate CSV File Before Upload </h1> <input type="file" onChange={handleFileChange} /> <p style={{ color: 'red' }}>{errorMessage}</p> </div> ); } ReactDOM.render(<App />, document.getElementById("app")); </script> </body> </html>