react与antd搭配实现图片上传与设置主图功能

本文介绍如何在React项目中结合Antd的Upload组件,自定义实现图片上传功能,并添加设置主图的选项。通过封装组件,处理fileList并结合radio按钮,实现了每张图片下方可选择为主图的需求。具体实现包括CSS样式和组件的使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

antd的upload可以实现上传,但是其上传之后,图片列表展示都是其组件里面已经封装好的,也没有vue那种提供插槽solt的功能,但是我司需要实现在每张图片下方有个radio标签来设置主图,左思右想,是不是得直接把upload组件拿过来再自己封装一层呢?实力不允许也没办法。

 so 我的解决办法是,封装一个组件,里面有upload组件,上传成功的话,将fileList filter出来,用图片展示,图片和radio可以封装的把,这并不难,直接放下代码把:

import React, { Component } from 'react';
import { Upload, message, Icon, Modal, Radio, Card } from 'antd';
import config from '../../../../config';
import { getCookie } from 'Utils/publicFunc';
import './UploadPhoto.scss';

class UploadPhoto extends Component {
    constructor(props) {
        super(props);
        this.state = {
            showModal: false,
            previewImage: '',
            fileList: [],
            value:''
        };
        this.handleChange = this.handleChange.bind(this);
        this.handleCancel = this.handleCancel.bind(this);
        this.beforeUpload = this.beforeUpload.bind(this);
        this.onChangeRadioSelect=this.onChangeRadioSelect.bind(this);
    }
    handleChange(info) {
        let { file, fileList } = info;
        if (file.status == 'error') {
            Modal.error({
                centered: true,
                title: '上传失败',
                content: file.response.message
            });
            return;
        }
        fileList = fileList.map((file) => {
            if (file.response) {
                file.url = file.response.downloadPath;
            }
            return file;
        });
        fileList = fileList.filter((file) => {
            return file.status === 'done';
        });
        this.setState({
            fileList: [...fileList]
        }, () => {
            if (file.status == 'done')
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值