Upload上传·续1

<​template>  <​div>    <​a-upload      action="https://www.mocky.io/v2/5cc8019d300000980a055e76"      :transform-file="transformFile"      v-model:file-list="fileList"    >      <​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';export default defineComponent({  components: {    UploadOutlined,  },  setup() {    const transformFile = (file: any) => {      return new Promise(resolve => {        const reader = new FileReader();        reader.readAsDataURL(file);        reader.onload = () => {          const canvas = document.createElement('canvas');          const img: HTMLImageElement = document.createElement('img');          img.src = reader.result as string;          img.onload = () => {            const ctx: CanvasRenderingContext2D = canvas.getContext('2d')!;            ctx.drawImage(img, 0, 0);            ctx.fillStyle = 'red';            ctx.textBaseline = 'middle';            ctx.fillText('Ant Design', 20, 20);            canvas.toBlob(resolve);          };        };      });    };    return {      transformFile,      fileList: ref([]),    };  },});<​/script>

Upload

用户头像

点击上传用户头像,并使用 beforeUpload 限制用户上传的图片格式和大小。

beforeUpload 的返回值可以是一个 Promise 以支持异步处理,如服务端校验等:示例。

<​template>  <​a-upload    v-model:file-list="fileList"    name="avatar"    list-type="picture-card"    class="avatar-uploader"    :show-upload-list="false"    action="https://www.mocky.io/v2/5cc8019d300000980a055e76"    :before-upload="beforeUpload"    @change="handleChange"  >    <​img v-if="imageUrl" :src="imageUrl" alt="avatar" />    <​div v-else>      <​loading-outlined v-if="loading"><​/loading-outlined>      <​plus-outlined v-else><​/plus-outlined>      <​div class="ant-upload-text">Upload<​/div>    <​/div>  <​/a-upload><​/template><​script lang="ts">import { PlusOutlined, LoadingOutlined } from '@ant-design/icons-vue';import { message } from 'ant-design-vue';import { defineComponent, ref } from 'vue';interface FileItem {  uid: string;  name?: string;  status?: string;  response?: string;  url?: string;  type?: string;  size: number;  originFileObj: any;}interface FileInfo {  file: FileItem;  fileList: FileItem[];}function getBase64(img: Blob, callback: (base64Url: string) => void) {  const reader = new FileReader();  reader.addEventListener('load', () => callback(reader.result as string));  reader.readAsDataURL(img);}export default defineComponent({  components: {    LoadingOutlined,    PlusOutlined,  },  setup() {    const fileList = ref([]);    const loading = ref<​boolean>(false);    const imageUrl = ref<​string>('');    const handleChange = (info: FileInfo) => {      if (info.file.status === 'uploading') {        loading.value = true;        return;      }      if (info.file.status === 'done') {        // Get this url from response in real world.        getBase64(info.file.originFileObj, (base64Url: string) => {          imageUrl.value = base64Url;          loading.value = false;        });      }      if (info.file.status === 'error') {        loading.value = false;        message.error('upload error');      }    };    const beforeUpload = (file: FileItem) => {      const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';      if (!isJpgOrPng) {        message.error('You can only upload JPG file!');      }      const isLt2M = file.size / 1024 / 1024 < 2;      if (!isLt2M) {        message.error('Image must smaller than 2MB!');      }      return isJpgOrPng && isLt2M;    };    return {      fileList,      loading,      imageUrl,      handleChange,      beforeUpload,    };  },});<​/script><​style>.avatar-uploader > .ant-upload {  width: 128px;  height: 128px;}.ant-upload-sel​ect-picture-card i {  font-size: 32px;  color: #999;}.ant-upload-sel​ect-picture-card .ant-upload-text {  margin-top: 8px;  color: #666;}<​/style>

照片墙

用户可以上传图片并在列表中显示缩略图。当上传照片数到达限制后,上传按钮消失。

<​template>  <​div class="clearfix">    <​a-upload      action="https://www.mocky.io/v2/5cc8019d300000980a055e76"      list-type="picture-card"      v-model:file-list="fileList"      @preview="handlePreview"    >      <​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>  <​/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);  });}interface FileItem {  uid: string;  name?: string;  status?: string;  response?: string;  percent?: number;  url?: string;  preview?: string;  originFileObj?: any;}interface FileInfo {  file: FileItem;  fileList: FileItem[];}export default defineComponent({  components: {    PlusOutlined,  },  setup() {    const previewVisible = ref<​boolean>(false);    const previewImage = ref<​string | undefined>('');    const fileList = ref<​FileItem[]>([      {        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',      },      {        uid: '-3',        name: 'image.png',        status: 'done',        url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',      },      {        uid: '-4',        name: 'image.png',        status: 'done',        url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',      },      {        uid: '-5',        name: 'image.png',        status: 'error',      },    ]);    const handleCancel = () => {      previewVisible.value = false;    };    const handlePreview = async (file: FileItem) => {      if (!file.url && !file.preview) {        file.preview = (await getBase64(file.originFileObj)) as string;      }      previewImage.value = file.url || file.preview;      previewVisible.value = true;    };    const handleChange = ({ fileList: newFileList }: FileInfo) => {      fileList.value = newFileList;    };    return {      previewVisible,      previewImage,      fileList,      handleCancel,      handlePreview,      handleChange,    };  },});<​/script><​style>/* you can make up upload button and sample style by using stylesheets */.ant-upload-sel​ect-picture-card i {  font-size: 32px;  color: #999;}.ant-upload-sel​ect-picture-card .ant-upload-text {  margin-top: 8px;  color: #666;}<​/style>

Click or drag file to this area to upload

Support for a single or bulk upload. Strictly prohibit from uploading company data or other band files

拖拽上传

把文件拖入指定区域,完成上传,同样支持点击上传。

设置 multiple 后,在 IE10+ 可以一次上传多个文件。