ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Ant Design Vue照片墙a-upload

2021-09-03 18:32:22  阅读:240  来源: 互联网

标签:Vue const upload fileList Ant Design file reader ref


<template>
    <div class="clearfix">
        {{ fileList }}

        <a-upload
            list-type="picture-card"
            v-model:file-list="fileList"
            @preview="handlePreview"
            @change="handleChange"
            :customRequest="customRequest"
        >
            <div v-if="fileList.length < 8">
                <plus-outlined />
                <div class="ant-upload-text">Upload</div>
            </div>
        </a-upload>

        <!-- 
            回填
        -->
        <a-modal
            :visible="previewVisible"
            :footer="null"
            @cancel="handleCancel"
        >
            <img alt="example" style="width: 100%" :src="previewImage" />
        </a-modal>

        <button @click="showWrite">回填</button>
    </div>
</template>
<script lang="ts">
import { PlusOutlined } from '@ant-design/icons-vue'
import { defineComponent, ref } from 'vue'

function getBase64(file: 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 defineComponent({
    components: {
        PlusOutlined,
    },
    setup() {
        // 全屏预览状态
        const previewVisible = ref<boolean>(false)
        // 预览的照片
        const previewImage = ref<string | undefined>('')
        // 存放显示的图片数组集合
        const fileList: any = ref([])
        // 取消预览
        const handleCancel = () => {
            previewVisible.value = false
        }
        // 打开预览
        const handlePreview = async file => {
            if (!file.url && !file.preview) {
                file.preview = (await getBase64(file.originFileObj)) as string
            }
            previewImage.value = file.url || file.preview
            previewVisible.value = true
        }

        // customRequest 可以解决一个问题
        function customRequest(mess) {
            console.log(mess)
        }

        // 更改文件上传的的状态,将uploading状态更改为done完成状态哈
        function handleChange(mess) {
            mess.file.status = 'done'
        }

        // 模拟数据回填
        function showWrite() {
            fileList.value = [
                {
                    // uid: '-1',
                    // name: 'image.png',
                    // status: 'done',
                    url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
                },
                {
                    // uid: '-2',
                    // name: 'image.png',
                    // status: 'done',
                    url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
                },
            ]
        }
        return {
            previewVisible,
            previewImage,
            fileList,
            showWrite,
            handleCancel,
            handlePreview,
            customRequest,
            handleChange,
        }
    },
})
</script>
<style>
/* you can make up upload button and sample style by using stylesheets */
.ant-upload-select-picture-card i {
    font-size: 32px;
    color: #999;
}

.ant-upload-select-picture-card .ant-upload-text {
    margin-top: 8px;
    color: #666;
}
</style>

标签:Vue,const,upload,fileList,Ant,Design,file,reader,ref
来源: https://www.cnblogs.com/IwishIcould/p/15220507.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有