Home >Web Front-end >JS Tutorial >How to convert files to base64 in react
How to convert files to base64 in React: 1. Install the ReactFileReader plug-in; 2. Introduce ReactFileReader; 3. Write the page method; 4. Get the base64 address of the uploaded image.
The operating environment of this article: Windows7 system, react17.0.1, Dell G3.
How does react convert files to base64?
react uploads files to base64
npm install react-file-reader --save
<reactfilereader> <button> <icon></icon> 选择文件 </button> </reactfilereader> // 获取上传的图片的base64地址 handleFiles = (files) => { console.log(files.base64); }
import ReactFileReader from 'react-file-reader';
handleFiles = files => { console.log(files) } <reactfilereader> <button>Upload</button> </reactfilereader> Response
HTML5 FileList
When base64 is true, React File Reader returns a JS Object including both the base64 files and the HTML5 FileList. You can access their values at Object.base64 or Object .fileList
handleFiles = (files) => { console.log(files.base64) } <reactfilereader> <button>Upload</button> </reactfilereader>
Response
multipleFiles={true}
["data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA", "data:image/png;base64,i..."]
multipleFiles={false}
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA..."
Access HTML5 FileList with base64={true}
handleFiles = (files) => { console.log(files.fileList) }
Recommended learning: "react video tutorial"
The above is the detailed content of How to convert files to base64 in react. For more information, please follow other related articles on the PHP Chinese website!