react上传文件转base64
前言:react有一个第三方插件 ReactFileReader可以实现这个功能。实现步骤:1.安装插件npm install react-file-reader --save2.代码引入import ReactFileReader from "react-file-reader";3.写页面方法<ReactFileReaderfileTypes={[".png",".jpg",".gif"
·
前言:
react有一个第三方插件 ReactFileReader 可以实现这个功能。
实现步骤:
1.安装插件
npm install react-file-reader --save
2.代码引入
import ReactFileReader from "react-file-reader";
3.写页面方法
<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);
}
api:官网入口
Usage
Import React File Reader
import ReactFileReader from 'react-file-reader';
Basic Use
handleFiles = files => {
console.log(files)
}
<ReactFileReader handleFiles={this.handleFiles}>
<button className='btn'>Upload</button>
</ReactFileReader>
Response
Base64
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 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)
}
更多推荐
已为社区贡献10条内容
所有评论(0)