javascript FileReader – parsing long file in chunks

FileReader API is asynchronous so you should handle it with block calls. A for loop wouldn’t do the trick since it wouldn’t wait for each read to complete before reading the next chunk. Here’s a working approach. function parseFile(file, callback) { var fileSize = file.size; var chunkSize = 64 * 1024; // bytes var offset … Read more

How to display a image selected from input type = file in reactJS

Hook version: const [image, setImage] = useState(null) const onImageChange = (event) => { if (event.target.files && event.target.files[0]) { setImage(URL.createObjectURL(event.target.files[0])); } } return ( <div> <input type=”file” onChange={onImageChange} className=”filetype” /> <img alt=”preview image” src={image}/> </div> ) Class version render() { return ( <div> <input type=”file” onChange={onImageChange} className=”filetype” /> <img alt=”preview image” src={this.state.image}/> <div/> ) } onImageChange … Read more