Ant Design Vue 上传图片二次视频封装

 图片上传 可多张 

组件部分

<template>
    <div class="clearfix">
        <a-upload list-type="picture-card" :multiple="true" @preview="handlePreview" :file-list="fileList1"
            :customRequest="uploadImage" :remove="deleteImage">
            <div v-if="fileList1.length < fileListNum">
                <a-icon type="plus" />
                <div class="ant-upload-text">
                    {{ fileListName }}
                </div>
            </div>
        </a-upload>
         <!--预览图-->
        <a-modal :visible="previewVisible" :footer="null" @cancel="handleCancel">
            <img alt="example" style="width: 100%" :src="previewImage" />
        </a-modal>
    </div>
</template>
<script>
import axios from 'axios'
function getBase64(file) {
    return new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = () => resolve(reader.result);
        reader.onerror = error => reject(error);
    });
}
export default {
    props: {
        fileListNum: {
            type: Number,//上传数量
        },
        fileListName: {
            type: String,//内容提示
        },
        fileList: {
            type: String,//图片列表 字符串
        }
    },
    data() {
        return {
            previewVisible: false,
            previewImage: '',
            fileList1: [],
            fielListName: '',
        };
    },
    watch: {
        fileList: {
            handler(newVal, oldVal) {
                console.log('传入图片', this.fileList);
                if (this.fileList.length > 0) {
                    this.fileList1 = this.fileList.split(',').map(i => {
                        return {
                            name: '图片',
                            status: 'done',
                            uid: i,
                            url: i
                        }
                    })
                } else {
                    this.fileList1 = []
                }
            },
            deep: true,
            immediate: true
        },
    },
    mounted() {
    },
    methods: {
        handleCancel() {
            this.previewVisible = false;
        },
        uploadImage(file) {
            const formData = new FormData();
            formData.append("uploadFile", file.file);
            this.saveFile(formData);
        },
        deleteImage(e) {
            let index = this.fileList1.findIndex((i) => {
                return i.uid === e.uid
            })
            this.fileList1.splice(index, 1)
            this.$emit('update:fileList', this.fileList1.map(i => {
                return i.url
            }).toString(','));
            console.log(this.fileList1.length, this.fileListNum, index, e, this.fileList1);
        },
        saveFile(formData) {
            axios.post('请求接口', formData)
                .then((res) => {
                    if (res.data.code == 200) {
                        // 存入列表
                        this.fileList1.push({
                            uid: new Date().getTime(),
                            name: '课程封面.png',
                            status: "done",
                            url: res.data.data,
                        });
                        this.previewImage = res.data.data
                        this.$emit('update:fileList', this.fileList1.map(i => {
                            return i.url
                        }).toString(','));
                        console.log(this.fileList1.map(i => {
                            return i.url
                        }).toString(','));
                    } else {
                        this.$message.error("图片要小于500kb,请压缩后上传");
                    }
                })
                .catch(function (error) {
                    // console.log(error);
                });
        },
        async handlePreview(file) {
            // console.log(file);
            if (!file.url && !file.preview) {
                file.preview = await getBase64(file.originFileObj);
            }
            this.previewImage = file.url || file.preview;
            this.previewVisible = true;
        },
        handleChange({ fileList1 }) {
            // console.log({ fileList1 });
            if (fileList1.length <= this.fileListNum) {
                this.fileList1 = fileList1;
            }
        },
    },
};
</script>

 页面部分  引入


                <Upload class="upload" :fileListNum="fileListNum" :fileListName="fileListName"
                    :fileList.sync="form.imageUrl" ref="uploadImg"></Upload>

视频 上传 与图片上传类似 区别预览播放

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值