antd组建——上传图片

  1. 用到的关键包
import ImgCrop from 'antd-img-crop';
  1. 文件上传的相关方法
const beforeUpload = (file) => {
                const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
                if (!isJpgOrPng) {
                        message.warn('头像必须是JPG/PNG格式');
                }
                const isLt2M = file.size / 1024 / 1024 < 2;
                if (!isLt2M) {
                        message.warn('上传的头像图片必须小于2MB');
                }
                return isJpgOrPng && isLt2M;
        }

        const changeLogo = (e: { code: number; message: string; data: any[]; }) => {
                if (e.code !== 200) {
                        // message.error('上传logo失败!');
                        console.log(`产品配置管理上传logo失败:${e.message}`);
                } else {
                        const params = {
                                objectField: {
                                        logo: e.data[0],
                                },
                                entpName: props.departmentList[0].entpName,
                                entpCode: props.departmentList[0].entpCode,
                        };
                        // 将上传到服务器返回的地址 通过调用后端接口 保存在数据库中
                        dispatch({
                                type: 'entpMember/editAmoeba',
                                payload: params,
                                callback: (result: any) => {
                                        debugger;
                                        console.log(result);
                                        if (result.code === 200) {
                                                message.success(result.message);
                                        }
                                },
                        })
                }
        }

        const getExtraData = (file) => {
                // 产品端 团队 上传文件到服务器的相关参数
                return {
                        fileDirPath: '/logo/',
                        registrationCode: 'entp-img',
                };
        };
  1. 文件上传用到的组件以及上传方法的使用
<ImgCrop>
  <Upload
           name="file"
           listType="picture"
           className="avatar-uploader"
           showUploadList={false}
           action="/api/file/upload"
           beforeUpload={beforeUpload}
           data={getExtraData}
           onSuccess={(e: any) => changeLogo(e)}
           >
       		{props.departmentList ? (
       			JSON.parse(props.departmentList[0].extendFieldObject).logo ? (                                              											  			
               		 <img className={styles.image} 
                     	src={getPhotoUrl(JSON.parse(props.departmentList[0].extendFieldObject).logo)}/>) : 
                     	(<div style={{ backgroundColor: color }} className={styles.imgDiv}>{props.departmentList[0].entpName.substring(0, 1)}</div>)
             	) : ''
             }
	</Upload>
</ImgCrop>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Antd Vue 提供了一个 Upload 组件,可以用于上传图片。 使用方法如下: 1. 安装 antd-vue 和 axios: ``` npm install antd-vue axios --save ``` 2. 引入 antd-vue 和 axios: ```js import Vue from 'vue' import Antd from 'ant-design-vue' import 'ant-design-vue/dist/antd.css' import axios from 'axios' Vue.use(Antd) Vue.prototype.$axios = axios ``` 3. 在组件使用 Upload 组件: ```html <template> <div> <a-upload :action="uploadUrl" :before-upload="beforeUpload" :on-success="onSuccess" :on-error="onError" :show-upload-list="false" > <a-button> <a-icon type="upload"></a-icon> Click to Upload </a-button> </a-upload> </div> </template> <script> export default { data() { return { uploadUrl: '/api/upload', } }, methods: { beforeUpload(file) { const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png' if (!isJpgOrPng) { this.$message.error('You can only upload JPG/PNG file!') } const isLt2M = file.size / 1024 / 1024 < 2 if (!isLt2M) { this.$message.error('Image must smaller than 2MB!') } return isJpgOrPng && isLt2M }, onSuccess(response) { this.$message.success('Upload success') console.log(response) }, onError(error) { this.$message.error('Upload failed') console.log(error) }, }, } </script> ``` 上面的代码,Upload 组件的 action 属性指定了上传图片的 URL,before-upload 属性用于校验上传的文件类型和大小,on-success 和 on-error 属性分别用于处理上传成功和上传失败的情况。 在 beforeUpload 方法,我们校验了上传的文件类型和大小,如果不符合要求,则会弹出错误提示。 在 onSuccess 方法,我们打印了上传成功后返回的数据,可以根据实际需求进行处理。 在 onError 方法,我们打印了上传失败的错误信息,可以根据实际需求进行处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值