Upload上传
Upload 上传
文件选择上传和拖拽上传控件。
何时使用
上传是将信息(网页、文字、图片、视频等)通过网页或者上传工具发布到远程服务器上的过程。
-
当需要上传一个或一些文件时。
-
当需要展现上传的进度时。
-
当需要使用拖拽交互时。
代码演示
Click to Upload
点击上传
经典款式,用户点击按钮弹出文件选择框。
<template> <a-upload v-model:file-list="fileList" name="file" :multiple="true" action="https://www.mocky.io/v2/5cc8019d300000980a055e76" :headers="headers" @change="handleChange" > <a-button> <upload-outlined></upload-outlined> Click to Upload </a-button> </a-upload></template><script lang="ts">import { message } from 'ant-design-vue';import { UploadOutlined } from '@ant-design/icons-vue';import { defineComponent, ref } from 'vue';interface FileItem { uid: string; name?: string; status?: string; response?: string; url?: string;}interface FileInfo { file: FileItem; fileList: FileItem[];}export default defineComponent({ components: { UploadOutlined, }, setup() { const handleChange = (info: FileInfo) => { if (info.file.status !== 'uploading') { console.log(info.file, info.fileList); } if (info.file.status === 'done') { message.success(`${info.file.name} file uploaded successfully`); } else if (info.file.status === 'error') { message.error(`${info.file.name} file upload failed.`); } }; const fileList = ref([]); return { fileList, headers: { authorization: 'authorization-text', }, handleChange, }; },});</script>
Upload
xxx.png
yyy.png
zzz.png
已上传的文件列表
使用 defaultFileList 设置已上传的内容。
<template> <a-upload action="https://www.mocky.io/v2/5cc8019d300000980a055e76" v-model:file-list="fileList"> <a-button> <upload-outlined></upload-outlined> Upload </a-button> </a-upload></template><script lang="ts">import { UploadOutlined } from '@ant-design/icons-vue';import { defineComponent, ref } from 'vue';interface FileItem { uid: string; name?: string; status?: string; response?: string; url?: string;}interface FileInfo { file: FileItem; fileList: FileItem[];}export default defineComponent({ components: { UploadOutlined, }, setup() { const fileList = ref<FileItem[]>([ { uid: '1', name: 'xxx.png', status: 'done', response: 'Server Error 500', // custom error message to show url: 'http://www.baidu.com/xxx.png', }, { uid: '2', name: 'yyy.png', status: 'done', url: 'http://www.baidu.com/yyy.png', }, { uid: '3', name: 'zzz.png', status: 'error', response: 'Server Error 500', // custom error message to show url: 'http://www.baidu.com/zzz.png', }, ]); const handleChange = ({ file, fileList }: FileInfo) => { if (file.status !== 'uploading') { console.log(file, fileList); } }; return { fileList, handleChange, }; },});</script>
Upload
xxx.png
完全控制的上传列表
使用 fileList 对列表进行完全控制,可以实现各种自定义功能,以下演示二种情况:
- 上传列表数量的限制。
- 读取远程路径并显示链接。
<template> <a-upload action="https://www.mocky.io/v2/5cc8019d300000980a055e76" :multiple="true" :file-list="fileList" @change="handleChange" > <a-button> <upload-outlined></upload-outlined> Upload </a-button> </a-upload></template><script lang="ts">import { UploadOutlined } from '@ant-design/icons-vue';import { defineComponent, ref } from 'vue';interface FileItem { uid: string; name?: string; status?: string; response?: Response; url: string;}interface FileInfo { file: FileItem; fileList: FileItem[];}export default defineComponent({ components: { UploadOutlined, }, setup() { const fileList = ref<FileItem[]>([ { uid: '-1', name: 'xxx.png', status: 'done', url: 'http://www.baidu.com/xxx.png', }, ]); const handleChange = (info: FileInfo) => { let resFileList = [...info.fileList]; // 1. Limit the number of uploaded files // Only to show two recent uploaded files, and old ones will be replaced by the new resFileList = resFileList.slice(-2); // 2. read from response and show file link resFileList = resFileList.map(file => { if (file.response) { // Component will show file.url as link file.url = file.response.url; } return file; }); fileList.value = resFileList; }; return { fileList, handleChange, }; },});</script>
图片列表样式
上传文件为图片,可展示本地缩略图。IE8/9 不支持浏览器本地缩略图展示(Ref),可以写 thumbUrl 属性来代替。
<template> <div> <a-upload action="https://www.mocky.io/v2/5cc8019d300000980a055e76" list-type="picture" v-model:file-list="fileList" > <a-button> <upload-outlined></upload-outlined> upload </a-button> </a-upload> <br /> <br /> <a-upload action="https://www.mocky.io/v2/5cc8019d300000980a055e76" list-type="picture" v-model:file-list="fileList1" class="upload-list-inline" > <a-button> <upload-outlined></upload-outlined> upload </a-button> </a-upload> </div></template><script lang="ts">import { UploadOutlined } from '@ant-design/icons-vue';import { defineComponent, ref } from 'vue';interface FileItem { uid: string; name?: string; status?: string; response?: string; url?: string; thumbUrl?: string;}export default defineComponent({ components: { UploadOutlined, }, setup() { const fileList = ref<FileItem[]>([ { uid: '-1', name: 'xxx.png', status: 'done', url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png', thumbUrl: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png', }, { uid: '-2', name: 'yyy.png', status: 'done', url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png', thumbUrl: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png', }, ]); const fileList1 = ref<FileItem[]>([ { uid: '-1', name: 'xxx.png', status: 'done', url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png', thumbUrl: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png', }, { uid: '-2', name: 'yyy.png', status: 'done', url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png', thumbUrl: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png', }, ]); return { fileList, fileList1, }; },});</script><style scoped>/* tile uploaded pictures */.upload-list-inline :deep(.ant-upload-list-item) { float: left; width: 200px; margin-right: 8px;}.upload-list-inline :deep(.ant-upload-animate-enter) { animation-name: uploadAnimateInlineIn;}.upload-list-inline :deep(.ant-upload-animate-leave) { animation-name: uploadAnimateInlineOut;}</style>
Upload Directory
文件夹上传
支持上传一个文件夹里的所有文件。
<template> <a-upload action="https://www.mocky.io/v2/5cc8019d300000980a055e76" directory> <a-button> <upload-outlined></upload-outlined> Upload Directory </a-button> </a-upload></template><script lang="ts">import { defineComponent } from 'vue';import { UploadOutlined } from '@ant-design/icons-vue';export default defineComponent({ components: { UploadOutlined, },});</script>
上传前转换文件
使用 beforeUpload 转换上传的文件(例如添加水印)。
