Skip to content

UploadImage 图片上传控件

图片上传控件,支持同时上传多个图片,支持预览

何时使用

  • 可单独使用,也可以嵌套在 FormItem 表单控件中使用(无需再次封装)

开发者注意事项

  • 对于一般无需用户登录凭证的可以直接使用,如需登陆凭证可以通过 headers 传递给组件

  • 组件提供了预览图片的功能,如果需要自定义预览功能可以通过 previewFile 方法进行设置

代码演示

案例一

  • 上传失败
  • 上传图片
vue
<script setup lang="ts">
import { ref } from 'vue';
import { UploadImage } from '@/library';
import img1 from '@/assets/images/1.jpg';

const fileList = ref([{ uid: '1', name: 'default_1', url: img1 }]);

function headers() {
  return { Authorization: 'Bearer 2d66abed-0ccd-426e-9512-54fa32f20b8d' };
}
</script>

<template>
  <UploadImage
    v-model:fileList="fileList"
    multiple
    method="post"
    action="/v1.0/file/upload"
    :maxCount="10"
    :headers="headers"
  />
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { UploadImage } from '@/library';
import img1 from '@/assets/images/1.jpg';

const fileList = ref([{ uid: '1', name: 'default_1', url: img1 }]);

function headers() {
  return { Authorization: 'Bearer 2d66abed-0ccd-426e-9512-54fa32f20b8d' };
}
</script>

<template>
  <UploadImage
    v-model:fileList="fileList"
    multiple
    method="post"
    action="/v1.0/file/upload"
    :maxCount="10"
    :headers="headers"
  />
</template>

案例二

  • 上传失败
  • 上传图片
vue
<script setup lang="ts">
import { ref } from 'vue';
import { UploadImage } from '@/library';
import img1 from '@/assets/images/1.jpg';

const fileList = ref([{ uid: '1', name: 'default_1', url: img1 }]);

function headers() {
  return { Authorization: 'Bearer 2d66abed-0ccd-426e-9512-54fa32f20b8d' };
}
</script>

<template>
  <UploadImage
    v-model:fileList="fileList"
    multiple
    disabled
    method="post"
    action="/v1.0/file/upload"
    :maxCount="10"
    :headers="headers"
  />
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { UploadImage } from '@/library';
import img1 from '@/assets/images/1.jpg';

const fileList = ref([{ uid: '1', name: 'default_1', url: img1 }]);

function headers() {
  return { Authorization: 'Bearer 2d66abed-0ccd-426e-9512-54fa32f20b8d' };
}
</script>

<template>
  <UploadImage
    v-model:fileList="fileList"
    multiple
    disabled
    method="post"
    action="/v1.0/file/upload"
    :maxCount="10"
    :headers="headers"
  />
</template>

UploadImage API

propNamedescriptiontypedefault value
action上传的路径string-
accept上传的文件类型stringimage/*
methodrequest methodstring*
maxSize限制图片的大小,0 表示不限制number-
maxCount最多可以上传多少个图片,0 表示不限制number-
multiple是否支持多张图片上传booleantrue
disabled是否禁用boolean-
headers上传时携带的请求头object-
fileList(v-model)文件列表UploadChangeParam["fileList"]-
previewFile自定义图片预览function(url) {}-

UploadImage 事件

eventNamedescriptioncallback
error图片上传异常function (error) {}

Released under the MIT License.