Ant Design Vue照片墙a-upload

发布时间:2022-01-14 发布网站:https://www.cnblogs.com/IwishIcould
前端之家收集整理的这篇文章主要介绍了Ant Design Vue照片墙a-upload前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
<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',]
        }
        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>

总结


以上是前端之家为你收集整理的Ant Design Vue照片墙a-upload全部内容,希望文章能够帮你解决Ant Design Vue照片墙a-upload所遇到的程序开发问题。

如果觉得前端之家网站内容还不错,欢迎将前端之家网站推荐给前端开发程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。