<template>
<div class="clearfix">
<a-upload v-model:file-list="fileList"
action="这里放上传的接口地址完整的
//如 action="https://4545.131.com//admin/upload"
accept="image/*"
list-type="picture-card" @preview="handlePreview">
<div v-if="fileList.length < 8">
<div style="margin-top: 8px">Upload</div>
</div>
</a-upload>
<a-modal :visible="previewVisible" :title="previewTitle" :footer="null" @cancel="handleCancel">
<img alt="example" style="width: 100%" :src="previewImage" />
</a-modal>
</div>
</template>
<script lang="ts" setup>
import { ref } from 'vue';
import type { UploadProps } from 'ant-design-vue';
const 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);
});
};
const previewVisible = ref(false);
const previewImage = ref('');
const previewTitle = ref('');
const fileList = ref<UploadProps['fileList']>([]);
const handleCancel = () => {
previewVisible.value = false;
previewTitle.value = '';
};
const handlePreview = async (file: UploadProps['fileList'][number]) => {
if (!file.url && !file.preview) {
file.preview = (await getBase64(file.originFileObj)) as string;
}
previewImage.value = file.url || file.preview;
previewVisible.value = true;
previewTitle.value = file.name || file.url.substring(file.url.lastIndexOf('/') + 1);
};
</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 Dessign vue3+ts a-upload的上传实现
于 2023-04-03 15:35:33 首次发布
该代码示例展示了如何在Vue应用中利用AntDesignVue的Upload组件进行图片上传操作。当文件列表长度小于8时,显示上传按钮。点击预览可以弹出Modal窗口展示图片,使用FileReader将文件转换为Base64格式用于预览。
摘要由CSDN通过智能技术生成