效果展示:
文件选择上传和拖拽上传控件Upload上传
页面代码部分:
import React, {useEffect, useState} from 'react' import { Breadcrumb, Upload, message, Image, BarcodeOutlined} from 'antd'; import { LoadingOutlined, PlusOutlined } from '@ant-design/icons'; function Add() { return ( <div> <Breadcrumb style={{ margin: '16px 0' }}> <Breadcrumb.Item>主页</Breadcrumb.Item> <Breadcrumb.Item>文章添加</Breadcrumb.Item> </Breadcrumb> <Upload name="img" listType="picture-card" className="avatar-uploader" showUploadList={false} action="https://www.mocky.io/v2/5cc8019d300000980a055e76" onChange={info=>handleChange(info)} > {imageUrl ? <img src={imageUrl} alt="avatar" style={{ width: '100%' }} /> : uploadButton} </Upload> </div> ) } export default Add
2、逻辑方法部分代码:
const [imageUrl, setImageUrl] = useState('') const [loading, setLoading] = useState(false) const uploadButton = ( <div> {loading ? <LoadingOutlined /> : <PlusOutlined />} <div style={{ marginTop: 8 }}>Upload</div> </div> ); const handleChange = info => { if (info.file.status === 'uploading') { setLoading(true) return; } if (info.file.status === 'done') { // Get this url from response in real world. setLoading(false) console.log(info) setImageUrl(info.file.response.url) } };
3、完整案例:
import React, {useEffect, useState} from 'react' import { Breadcrumb, Upload, message, Image, BarcodeOutlined} from 'antd'; import { LoadingOutlined, PlusOutlined } from '@ant-design/icons'; function Add() { const [imageUrl, setImageUrl] = useState('') const [loading, setLoading] = useState(false) const uploadButton = ( <div> {loading ? <LoadingOutlined /> : <PlusOutlined />} <div style={{ marginTop: 8 }}>Upload</div> </div> ); const handleChange = info => { if (info.file.status === 'uploading') { setLoading(true) return; } if (info.file.status === 'done') { // Get this url from response in real world. setLoading(false) console.log(info) setImageUrl(info.file.response.url) } }; return ( <div> <Breadcrumb style={{ margin: '16px 0' }}> <Breadcrumb.Item>主页</Breadcrumb.Item> <Breadcrumb.Item>文章添加</Breadcrumb.Item> </Breadcrumb> <Upload name="img" listType="picture-card" className="avatar-uploader" showUploadList={false} action="https://www.mocky.io/v2/5cc8019d300000980a055e76" onChange={info=>handleChange(info)} > {imageUrl ? <img src={imageUrl} alt="avatar" style={{ width: '100%' }} /> : uploadButton} </Upload> </div> ) } export default Add
文章评论(0)