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 |
import React, { useRef, useState } from 'react' import { Col, Container, Form, Row } from 'react-bootstrap' import "bootstrap/dist/css/bootstrap.min.css" import { renderAsync } from 'docx-preview' function App() { const [error,setError] = useState("") const viewerRef = useRef(null) const handleFileChange = async (e) => { // read the docx file which is selected const file = e.target.files[0] if (!file || file.type !== "application/vnd.openxmlformats-officedocument.wordprocessingml.document") { setError("Please upload a valid .docx file."); return; } setError("") const arrayBuffer = await file.arrayBuffer() renderAsync(arrayBuffer,viewerRef.current).catch(() => { setError("An error occured rendering the document") }) } return ( <Container className='mt-4'> <Row className='mb-3'> <Col> <h1>Word Document Viewer</h1> </Col> </Row> <Row className='mb-3'> <Col> <Form> <Form.Group> <Form.Label>Upload a Word Document(.docx)</Form.Label> <Form.Control type='file' accept='.docx' onChange={handleFileChange} /> </Form.Group> </Form> </Col> </Row> <Row> <Col> <div ref={viewerRef} style={{ border:"1px solid #ccc", padding:"16px", height:"500px", overflow:"scroll" }} > {/* here the docx file will show */} </div> </Col> </Row> </Container> ) } export default App |