react實作檔案轉base64的方法:1、安裝ReactFileReader外掛程式;2、引入ReactFileReader;3、書寫好頁面方法;4、取得上傳的圖片的base64位址。
本文操作環境:Windows7系統、react17.0.1、Dell G3。
react怎麼實現檔案轉base64?
react上傳檔案轉base64
npm install react-file-reader --save
<ReactFileReader fileTypes={[".png",".jpg",".gif", "jpeg"]} base64 multipleFiles={!1} handleFiles={this.handleFiles}> <Button> <Icon type="upload" /> 选择文件 </Button> </ReactFileReader> // 获取上传的图片的base64地址 handleFiles = (files) => { console.log(files.base64); }
import ReactFileReader from 'react-file-reader';
handleFiles = files => { console.log(files) } <ReactFileReader handleFiles={this.handleFiles}> <button className='btn'>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 Objectbase.64 or can access their values at . .fileList
handleFiles = (files) => { console.log(files.base64) } <ReactFileReader fileTypes={[".csv",".zip"]} base64={true} multipleFiles={true} handleFiles={this.handleFiles}> <button className='btn'>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) }
推薦學習:《react影片教學》
#以上是react要怎麼實作檔案轉base64的詳細內容。更多資訊請關注PHP中文網其他相關文章!