reactjs - 从上传中获取 base 64 信息
问题描述
我有一个来自 ant 设计库的上传组件。
import React, {useState} from 'react';
import ReactDOM from 'react-dom';
import 'antd/dist/antd.css';
import './index.css';
import {
Form,
Select,
InputNumber,
Switch,
Radio,
Slider,
Button,
Upload,
Rate,
Checkbox,
Row,
Col,
} from 'antd';
import { UploadOutlined, InboxOutlined } from '@ant-design/icons';
const { Option } = Select;
const formItemLayout = {
labelCol: {
span: 6,
},
wrapperCol: {
span: 14,
},
};
const normFile = e => {
console.log('Upload event:', e);
if (Array.isArray(e)) {
return e;
}
return e && e.fileList;
};
const Demo = () => {
const onFinish = values => {
console.log('Received values of form: ', values);
};
const [state, setState] = useState({
loading: false,
});
function getBase64(img, callback) {
const reader = new FileReader();
reader.addEventListener('load', () => callback(reader.result));
reader.readAsDataURL(img);
}
const handleChange = info => {
if (info.file.status === 'uploading') {
setState({loading: true});
return;
}
if (info.file.status === 'done') {
// Get this url from response in real world.
getBase64(info.file.originFileObj, imageUrl =>
setState({
imageUrl,
loading: false,
}),
);
}
};
function beforeUpload(file) {
const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
if (!isJpgOrPng) {
console.error('You can only upload JPG/PNG file!');
}
const isLt2M = file.size / 1024 / 1024 < 2;
if (!isLt2M) {
console.error('Image must smaller than 2MB!');
}
return isJpgOrPng && isLt2M;
}
const uploadButton = (
<div>
<div className="ant-upload-text">Upload img</div>
</div>
);
const {imageUrl} = state;
return (
<Form
name="validate_other"
{...formItemLayout}
onFinish={onFinish}
initialValues={{
'input-number': 3,
'checkbox-group': ['A', 'B'],
rate: 3.5,
}}
>
<Form.Item
name="upload"
label="Upload"
valuePropName="fileList"
getValueFromEvent={normFile}
>
<Upload
listType="picture-card"
className="avatar-uploader"
showUploadList={false}
action="https://www.mocky.io/v2/5cc8019d300000980a055e76"
beforeUpload={beforeUpload}
onChange={handleChange}
>
{imageUrl ? <img src={imageUrl} alt="avatar" style={{width: '100%'}}/> : uploadButton}
</Upload>
</Form.Item>
<Form.Item
wrapperCol={{
span: 12,
offset: 6,
}}
>
<Button type="primary" htmlType="submit">
get data from upload
</Button>
</Form.Item>
</Form>
);
};
ReactDOM.render(<Demo />, document.getElementById('container'));
现在,当我上传图像并单击upload img
按钮后,我遇到了问题。我没有在console.log('Received values of form: ', values);
问题中获得图像的基本 64 位数据:我什么时候单击该按钮以获取数据base64
?
演示:https ://codesandbox.io/s/silent-grass-sqzl1?file=/index.js:0-2944
解决方案
推荐阅读
- sql - 2 条件 1 AND 语句
- azure-devops - 如何将 ASPNET Core 站点部署到 Azure Linux Web App(无容器)
- c# - 列表框多选模式不起作用
- c# - 我可以使用抽象基类作为 Unity Editor 元素吗?
- angular - 在 ag-grid 中加载数据时如何避免“无行”消息
- php - 来自 mysqli 数据库的 Div 数组未显示第一行
- c# - Sql Server 中的部分搜索?
- sublimetext3 - sublime text actualvim 如何将路径添加到neovim?
- java - Docker:Java 无法解析主机 IP 地址
- caching - 如何将 Service Worker 与 Yeoman webapp 生成器一起使用?