react+antd实现图片上传预览功能

2021-08-21   阅读:4931   类型:前端   分类:React    标签: React

效果展示:

文件选择上传和拖拽上传控件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
腾讯云11.11上云拼团Go,2核2G3M云服务器 28元/年

‘简忆博客’微信公众号 扫码关注‘简忆博客’微信公众号,获取最新文章动态
转载:请说明文章出处“来源简忆博客”。http://tpxhm.com/fdetail/794.html

×
觉得文章有用就打赏一下文章作者
微信扫一扫打赏 微信扫一扫打赏
支付宝扫一扫打赏 支付宝扫一扫打赏

文章评论(0)

登录
简忆博客壁纸一
简忆博客壁纸二
简忆博客壁纸三
简忆博客壁纸四
简忆博客壁纸五
简忆博客壁纸六
简忆博客壁纸七
简忆博客壁纸八
头像

简忆博客
勤于学习,乐于分享

置顶推荐

打赏本站

如果你觉得本站很棒,可以通过扫码支付打赏哦!
微信扫码:你说多少就多少~
微信扫码
支付宝扫码:你说多少就多少~
支付宝扫码
×