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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | import React, { useState } from "react"; import FileViewer from "react-file-viewer"; import { Container, Row, Col, Form } from "react-bootstrap"; import "bootstrap/dist/css/bootstrap.min.css"; const App = () => { const [fileType, setFileType] = useState("image"); const [file, setFile] = useState(null); const handleFileTypeChange = (e) => { setFileType(e.target.value); setFile(null); // Reset file when type changes }; const handleFileChange = (e) => { const selectedFile = e.target.files[0]; if (selectedFile) { const fileURL = URL.createObjectURL(selectedFile); setFile(fileURL); } }; const renderFileViewer = () => { if (!file) { return <p className="text-muted">No file selected. Please upload a file.</p>; } if (["pdf", "docx"].includes(fileType)) { return ( <FileViewer fileType={fileType} filePath={file} errorComponent={() => <p>Error loading file</p>} /> ); } if (["image"].includes(fileType)) { return <img src={file} alt="Uploaded File" style={{ maxWidth: "100%" }} />; } if (["video"].includes(fileType)) { return <video controls style={{ width: "100%" }} src={file} />; } if (["audio"].includes(fileType)) { return <audio controls src={file} />; } return <p className="text-danger">Unsupported file type selected.</p>; }; return ( <Container className="mt-4"> <Row> <Col> <h1>File Viewer</h1> </Col> </Row> <Row className="mb-3"> <Col> <Form> <Form.Label>Select File Type:</Form.Label> <div> <Form.Check inline label="Image" type="radio" value="image" checked={fileType === "image"} onChange={handleFileTypeChange} /> <Form.Check inline label="Video" type="radio" value="video" checked={fileType === "video"} onChange={handleFileTypeChange} /> <Form.Check inline label="Audio" type="radio" value="audio" checked={fileType === "audio"} onChange={handleFileTypeChange} /> <Form.Check inline label="PDF" type="radio" value="pdf" checked={fileType === "pdf"} onChange={handleFileTypeChange} /> <Form.Check inline label="DOCX" type="radio" value="docx" checked={fileType === "docx"} onChange={handleFileTypeChange} /> </div> </Form> </Col> </Row> {fileType && ( <Row className="mb-3"> <Col> <Form> <Form.Label>Select a File:</Form.Label> <Form.Control type="file" accept={ fileType === "image" ? "image/*" : fileType === "video" ? "video/*" : fileType === "audio" ? "audio/*" : fileType === "pdf" ? ".pdf" : fileType === "docx" ? ".docx" : "" } onChange={handleFileChange} /> </Form> </Col> </Row> )} <Row> <Col> <h4>File Preview:</h4> <div style={{ border: "1px solid #ddd", padding: "10px" }}> {renderFileViewer()} </div> </Col> </Row> </Container> ); }; export default App; |