App.jsx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
import React, { useState } from "react"; import { Container, Form, Button, Alert } from "react-bootstrap"; import { ReactSVG } from "react-svg"; import "bootstrap/dist/css/bootstrap.min.css" const App = () => { const [svgFile, setSvgFile] = useState(null); const [error, setError] = useState(""); const handleFileUpload = (e) => { const file = e.target.files[0]; if (!file) { setError("No file selected."); return; } if (!file.name.endsWith(".svg")) { setError("Please upload a valid SVG file."); return; } setError(""); setSvgFile(URL.createObjectURL(file)); // Create a local URL for the SVG file }; return ( <Container className="my-4"> <h1 className="text-center">React SVG Viewer</h1> {/* File Upload */} <Form.Group controlId="svgFile" className="my-3"> <Form.Label>Upload an SVG File</Form.Label> <Form.Control type="file" accept=".svg" onChange={handleFileUpload} /> </Form.Group> {error && <Alert variant="danger">{error}</Alert>} {/* Display SVG */} {svgFile && ( <div className="text-center mt-4"> <h5>Preview:</h5> <ReactSVG src={svgFile} beforeInjection={(svg) => { svg.setAttribute("style", "width: 100%; height: auto;"); }} className="svg-viewer" /> </div> )} </Container> ); }; export default App; |