This is the second day of my participation in the August Text Challenge.More challenges in August
Realize the function
- Components are developed based on the React-typescript project
- Is about importing Excel file format concrete implementation
- Support for click and drag imports
- After importing, parse the document data into JSON format. Click OK to submit THE JSON data to the background through the interface
- Component preview (UI reference elementUI drag and drop upload component)
implementation
The principle of
- use
FileReader
The object’sFileReader.readAsText()
Method to read file information to parseFile
Objects can be from the user in oneinput
Element after the file is selectedFileList
Objects can also be generated from drag-and-drop operationsDataTransfer
Object that fires after reading is completeFileReader.onload
Method, the parse read result isjson
Format data
Train of thought
- Click Upload:
- Manually triggered when the drag area is clicked
input
theclick
Event, throughdisplay:none
Hidden truthinput
The element
// basic-upload.tsx // Manually trigger the input click event const inputEl = useRef<HTMLInputElement>(null) const handleClick = () = > { if (inputEl.current) { inputEl.current.click() } } return ( <> <UploadDragger onClick={handleClick}> <div className='drag-tip'> <CloudUploadIcon /> <div>Drag the file here, or click Upload</div> </div> </UploadDragger> <div className={classes.uploadTip}>To upload only files that match the template, click<span className={classes.downloadTemp} onClick={downloadTemp}>Download the template</span> </div> <input ref={inputEl} className={classes.inputInvisible} type='file' onChange={handleChange} accept={'png'} ></input> </> ) Copy the code
- Listening to the
input
thechange
Event, parse the file
// basic-upload.tsx // Upload the parse file const handleChange = (evt: ChangeEvent<HTMLInputElement>) = > { let files = evt.target.files / / File object const reader = new FileReader() // FileReader object instance reader.onload = function fileReadCompleted() { let result = analyseBrokerFile(reader) // The analyseBrokerFile method parses the file as JSON data. }if (files) reader.readAsText(files[0].'gbk') // GBK code asynchronously reads files } Copy the code
- Manually triggered when the drag area is clicked
- Drag and drop upload and parse
- Listening to the
onDrop
The event,e.dataTransfer.files
Obtain file information and parse it into A JSON file in the same way as above
const UploadDragger = (props:PropType) = > { const { onClick } = props const classes = draggerStyles() const onDrop = (e: React.DragEvent) = > { e.preventDefault() let files= e.dataTransfer.files const reader = new FileReader() reader.onload = function fileReadCompleted() { let data = analyseBrokerFile(reader) ... } if (files) reader.readAsText(files[0].'gbk')}return ( <div onClick={onClick} className={classes.dragger} onDrop={onDrop} onDragOver={onDragOver} onDragLeave={onDragLeave}> {props.children} </div>)}export default UploadDragger Copy the code
- Listening to the
- Analytical method:
analyseBrokerFile
Methods are defined by themselves, according to different projects, differentExcel
The contents of the file are resolved to differentjson
The data. When the file is read, the contents of the reader.result file are filtered and processed by means of regex, traversal, etc
conclusion
- Drag and drop is implemented in the same way as single click upload, but the listening events are different
- In addition, you can add some other features, such as upload progress, multi-file upload, file format filtering judgment, file size limits, and so on
reference
- MDN