Uploader
Examples
File upload
Set the value of type
to file
to enter file upload mode.
Normal size
- demo-file1.txt
- demo-file2.txt
Small size
- demo-file1.txt
- demo-file2.txt
<template>
<article>
<section>
<h4>Normal size</h4>
<veui-uploader
v-model="files"
action="/api/upload"
/>
</section>
<section>
<h4>Small size</h4>
<veui-uploader
v-model="files2"
action="/api/upload"
ui="s"
/>
</section>
</article>
</template>
<script>
import { Uploader } from 'veui'
import { cloneDeep } from 'lodash'
export default {
components: {
'veui-uploader': Uploader
},
data () {
let files = [
{
name: 'demo-file1.txt',
src: '/file/demo-file1.txt',
key: 0
},
{
name: 'demo-file2.txt',
src: '/file/demo-file2.txt',
key: 1
}
]
return {
files,
files2: cloneDeep(files)
}
}
}
</script>
<style lang="less" scoped>
article {
display: flex;
}
section {
width: 45%;
}
</style>
Image upload
Set the value of type
to image
to enter image upload mode.
Normal size
- Normal size
Small size
- Small size
<template>
<article>
<section>
<h4>Normal size</h4>
<veui-uploader
v-model="images"
type="image"
action="/api/upload"
>
<template #help>
Normal size
</template>
</veui-uploader>
</section>
<section>
<h4>Small size</h4>
<veui-uploader
v-model="images2"
type="image"
action="/api/upload"
ui="s"
>
<template #help>
Small size
</template>
</veui-uploader>
</section>
</article>
</template>
<script>
import { Uploader } from 'veui'
import { cloneDeep } from 'lodash'
export default {
components: {
'veui-uploader': Uploader
},
data () {
let images = [
{
src: '/images/development/uploader/demo-image1.jpg',
key: 0
},
{
src: '/images/development/uploader/demo-image2.jpg',
key: 1
}
]
return {
images,
images2: cloneDeep(images)
}
}
}
</script>
Media upload
Set the value of type
to media
to enter media upload mode.
Normal size
- Normal size
Small size
- Small size
<template>
<article>
<section>
<h4>Normal size</h4>
<veui-uploader
v-model="media"
type="media"
action="/api/upload"
>
<template #help>
Normal size
</template>
</veui-uploader>
</section>
<section>
<h4>Small size</h4>
<veui-uploader
v-model="media2"
type="media"
action="/api/upload"
ui="s"
>
<template #help>
Small size
</template>
</veui-uploader>
</section>
</article>
</template>
<script>
import { Uploader } from 'veui'
import { cloneDeep } from 'lodash'
export default {
components: {
'veui-uploader': Uploader
},
data () {
let media = [
{
type: 'image',
src: '/images/development/uploader/demo-image1.jpg',
key: 0
},
{
type: 'video',
src: 'https://nadvideo2.baidu.com/5dafd8544f4f53b27a5f59b0ab780403_1920_1080.mp4',
poster: 'https://feed-image.baidu.com/0/pic/4dced79d185a16e228652b136f653dcc.jpg',
key: 1
}
]
return {
media,
media2: cloneDeep(media)
}
}
}
</script>
Validation
Validate image format, size, quantity, and use validator
for custom validation.
- 请选择jpg、png图片,大小不超过100kb,宽高分别大于200px,最多上传6张图片
<template>
<article>
<section>
<veui-uploader
v-model="images"
type="image"
action="/api/upload"
accept="jpg,png"
:max-count="6"
max-size="100kb"
:validator="validator"
>
<template #help>
请选择jpg、png图片,大小不超过100kb,宽高分别大于200px,最多上传6张图片
</template>
</veui-uploader>
</section>
</article>
</template>
<script>
import { Uploader } from 'veui'
export default {
components: {
'veui-uploader': Uploader
},
data () {
return {
images: [
{
src: '/images/development/uploader/demo-image1.jpg',
key: 0
},
{
src: '/images/development/uploader/demo-image2.jpg',
key: 1
}
],
validator (file) {
return new Promise(resolve => {
let image = new Image()
image.src = window.URL.createObjectURL(file)
image.onload = () => {
resolve({
valid: image.height > 200 && image.width > 200,
message: '图片宽高太小'
})
}
})
}
}
}
}
</script>
Custom upload
Customize the upload process using upload
and configure the overlay options using controls
.
- 点击图片浮层上的向右箭头按钮改变图片位置
<template>
<article>
<section>
<veui-uploader
v-model="images"
type="image"
:action="action"
:controls="controls"
request-mode="custom"
:upload="upload"
@moveright="handleMoveRight"
>
<template #help>
点击图片浮层上的向右箭头按钮改变图片位置
</template>
</veui-uploader>
</section>
</article>
</template>
<script>
import { Uploader } from 'veui'
export default {
components: {
'veui-uploader': Uploader
},
data () {
return {
action: '/upload',
images: [
{
key: 0,
src: '/images/development/uploader/demo-image1.jpg'
},
{
key: 1,
src: '/images/development/uploader/demo-image2.jpg'
}
],
controls (file, defaultControls) {
if (file.status === 'success') {
return [
{ name: 'moveright', icon: 'chevron-right', disabled: false },
...defaultControls
]
}
return defaultControls
},
upload: (file, { onload, onprogress, onerror }) => {
let xhr = new XMLHttpRequest()
file.xhr = xhr
xhr.upload.onprogress = e => onprogress(e)
xhr.onload = e => {
try {
onload(JSON.parse(xhr.responseText))
} catch (e) {
onload({ success: false, message: e })
}
}
xhr.onerror = e => onerror(e)
let formData = new FormData()
formData.append('file', file)
xhr.open('POST', this.action, true)
xhr.send(formData)
return () => {
xhr.abort()
}
}
}
},
methods: {
handleMoveRight (file, index) {
if (index < this.images.length - 1) {
let temp = { ...this.images[index] }
this.$set(this.images, index, this.images[index + 1])
this.$set(this.images, index + 1, temp)
}
}
}
}
</script>
Sorting
Set the sortable
property to specify that upload items can be sorted by drag and drop.
<template>
<article>
<section>
<veui-uploader
v-model="images"
sortable
type="image"
action="/api/upload"
/>
</section>
</article>
</template>
<script>
import { Uploader } from 'veui'
export default {
components: {
'veui-uploader': Uploader
},
data () {
return {
images: [
{
key: 0,
src: '/images/development/uploader/demo-image1.jpg'
},
{
key: 1,
src: '/images/development/uploader/demo-image2.jpg'
}
]
}
}
}
</script>
Custom action bar
Set the controls
property to specify the action options that appear when hovering over each upload item.
<template>
<article>
<section>
<veui-uploader
v-model="images"
:controls="controls"
type="image"
action="/api/upload"
@star="handleStar"
/>
</section>
</article>
</template>
<script>
import { Uploader, toast } from 'veui'
import 'veui-theme-dls-icons/star'
export default {
components: {
'veui-uploader': Uploader
},
data () {
return {
images: [
{
key: 0,
src: '/images/development/uploader/demo-image1.jpg'
},
{
key: 1,
src: '/images/development/uploader/demo-image2.jpg'
}
],
controls (item, defaultControls) {
return [
...defaultControls,
{
name: 'star',
icon: 'star',
label: 'star'
}
]
}
}
},
methods: {
handleStar () {
toast.info('You click star!')
}
}
}
</script>
Custom upload entry
Set the entries
property to specify the action options that appear when hovering over "continue uploading" items.
<template>
<article>
<section>
<veui-uploader
v-model="images"
:entries="entries"
type="image"
action="/api/upload"
@star="handleStar"
/>
</section>
</article>
</template>
<script>
import { Uploader, toast } from 'veui'
import 'veui-theme-dls-icons/star'
export default {
components: {
'veui-uploader': Uploader
},
data () {
return {
images: [
{
key: 0,
src: '/images/development/uploader/demo-image1.jpg'
},
{
key: 1,
src: '/images/development/uploader/demo-image2.jpg'
}
],
entries (defaultEntries) {
return [
...defaultEntries,
{
name: 'star',
icon: 'star',
label: 'star'
}
]
}
}
},
methods: {
handleStar () {
toast.info('You clicked star!')
}
}
}
</script>
Custom pick
Set the pick
property to customize file selection.
<template>
<article>
<section>
<veui-uploader
type="file"
:pick="pick"
action="/api/upload"
/>
</section>
</article>
</template>
<script>
import { Uploader } from 'veui'
import confirm from 'veui/plugins/confirm'
import Vue from 'vue'
Vue.use(confirm)
export default {
components: {
'veui-uploader': Uploader
},
methods: {
pick ({ remainingCount }) {
return this.$confirm('模拟自定义选择文件', '确认').then((ready) => {
return ready
? new File([`count: ${remainingCount}`], 'pick.txt', { type: 'text/plain' })
: null
})
}
}
}
</script>
Help text
Set the help
property or help
slot to specify help text.
- demo-file1.txt
- demo-file1.txt
<template>
<article>
<section>
<div>
辅助信息位置:
<veui-radio-button-group
v-model="helpPosition"
ui="s"
:items="availableHelpPositions"
/>
</div>
<veui-uploader
v-model="files"
class="uploader"
:help-position="helpPosition"
help="Help text"
action="/api/upload"
/>
<veui-uploader
v-model="files"
class="uploader"
:help-position="helpPosition"
action="/api/upload"
>
<template #help>
Custom help text via `help` slot
</template>
</veui-uploader>
</section>
</article>
</template>
<script>
import { Uploader, RadioButtonGroup } from 'veui'
const mapper = (value) => ({ label: value, value })
export default {
components: {
'veui-uploader': Uploader,
'veui-radio-button-group': RadioButtonGroup
},
data () {
let files = [
{
name: 'demo-file1.txt',
src: '/file/demo-file1.txt',
key: 0
}
]
return {
files,
helpPosition: 'side',
availableHelpPositions: ['side', 'bottom'].map(mapper)
}
}
}
</script>
<style lang="less" scoped>
article {
display: flex;
}
.uploader {
margin-top: 16px;
}
section {
width: 45%;
}
</style>
API
Props
Name | Type | Default | Description | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ui | string | - | Predefined styles.
| ||||||||||||||||||
type | string | 'file' | The type of file to be uploaded.
| ||||||||||||||||||
value | Object | Array<Object> | - | When Each file object is of type | ||||||||||||||||||
key-field | string | 'key' | Specifies the unique key of the file object, which serves as the basis for correctly handling the order of the file list when the data changes. | ||||||||||||||||||
name | string | 'file' | The name of the native <input> element. | ||||||||||||||||||
action | string | - | The upload URL. | ||||||||||||||||||
headers | Object | uploader.headers | The content to be added to the HTTP request headers. Can be globally configured. | ||||||||||||||||||
with-credentials | boolean | true | Same as the withCredentials property of XMLHttpRequest . | ||||||||||||||||||
request-mode | string | uploader.requestMode | Specifies the asynchronous upload method. Can be globally configured.
| ||||||||||||||||||
iframe-mode | string | uploader.iframeMode | When the value of
| ||||||||||||||||||
callback-namespace | string | uploader.callbackNamespace | Specifies the namespace of the callback function when request-mode is 'iframe' and iframe-mode is 'callback' , under the window object. Can be globally configured. | ||||||||||||||||||
data-type | string | 'json' | Specifies the parsing method for text if the callback value is text.
| ||||||||||||||||||
convert-response | uploader.convertResponse | - | A function that converts callback data to the required format so that the component can determine whether the upload was successful and display the corresponding result. The parameter is the callback data. The format of the returned result is as follows:
Custom attributes can also be added to the returned result. These custom attributes will be included in the | ||||||||||||||||||
accept | string | - | Same as the accept attribute of the native <input> element, adding another layer of validation after the browser's file type filtering. For cases where the MIME type, such as application/msword , does not match the extension, the validation is skipped. | ||||||||||||||||||
max-count | number | - | The maximum number of files. | ||||||||||||||||||
max-size | number | string | - | The maximum size of a single file. If it is a number , the unit is in bytes ; if it is a string , it supports adding units after the number, and the units can be b / kb / mb / gb / tb . | ||||||||||||||||||
validator | function(Object): Object | Promise<Object> | - | Custom validation logic, with the original
Asynchronous validation is supported, and the return value can be a | ||||||||||||||||||
payload | Object | - | Additional parameters to be carried in the upload request. | ||||||||||||||||||
autoupload | boolean | true | Specifies whether to upload immediately after selecting files. | ||||||||||||||||||
order | string | 'asc' | The display order of uploaded files, sorted by the time of upload start.
| ||||||||||||||||||
picker-position | string | 'after' | In image upload mode, the position of the upload button in the image queue.
| ||||||||||||||||||
upload | function(Object, Object): function | - | Customize the upload process when the value of
If | ||||||||||||||||||
controls | function(Object, Array<Object>): Array<Object> | - | In image upload mode, customize the operation items on the image mask layer. The parameters are
| ||||||||||||||||||
multiple | boolean | false | Upload multiple files. When max-count is 1 and multiple is true , value is also an array. | ||||||||||||||||||
entries | function(Array<Object>): Array<Object> | - | Control the operation items for each uploaded file, such as delete and preview. The default operation item array will be passed as a parameter, and the actual effective operation item array will be returned. The operation item object refers to the field details of the | ||||||||||||||||||
pick | function({remainingCount: number}): Promise<Object> | - | Custom logic for selecting files to be uploaded.
| ||||||||||||||||||
after-pick | function(Array<Object>): void | - | The callback after selecting files. | ||||||||||||||||||
sortable | boolean | false | Whether the file list can be sorted. | ||||||||||||||||||
preview-options | object | {wrap: true, indicator: 'number'} | Preview options passed to Lightbox . | ||||||||||||||||||
validity-display | 'popup' | 'inline' | 'popup' | Display method for validation information.
| ||||||||||||||||||
help | string | - | Help text. | ||||||||||||||||||
help-position | 'side' | 'bottom' | 'side' | Display position for help information.
| ||||||||||||||||||
picker-label | string | - | The text of the upload button. | ||||||||||||||||||
picker-icon | string | Object | - | The icon of the upload button, referring to the name prop of Icon . |
Slots
Name | Description | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
upload | The area of the upload button in image upload mode. | |||||||||||||||
help | Tips for file quantity, format, size, etc. | |||||||||||||||
file | The area of a single file used to customize its content.
| |||||||||||||||
file-before | The area before the content of a single file. The slot parameters are the same as those of the file slot. | |||||||||||||||
file-after | The area after the content of a single file. The slot parameters are the same as those of the file slot. | |||||||||||||||
uploading | The area of a single image being uploaded in image upload mode. The slot parameters are the same as those of the file slot. | |||||||||||||||
failure | The area of a single image that failed to upload in image upload mode. The slot parameters are the same as those of the file slot). |
Events
Name | Description | |||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
change | Triggered only when uploading or deleting files, the callback parameter is
| |||||||||||||||||||||||||||||
remove | Triggered when a file is deleted, the callback parameter is
Also contains custom properties added in | |||||||||||||||||||||||||||||
success | Triggered when uploading is successful, the callback parameter is the same as that of the remove event. | |||||||||||||||||||||||||||||
failure | Triggered when uploading fails, the callback parameter is the same as that of the remove event. | |||||||||||||||||||||||||||||
invalid | Triggered when file validation fails, the callback parameter is
| |||||||||||||||||||||||||||||
statuschange | Triggered when the overall status of all files changes, the callback parameter is the overall status of the component
| |||||||||||||||||||||||||||||
progress | Triggered when the upload progress changes in the case where the value of
|
Methods
Name | Description |
---|---|
startUpload | Manually start uploading when autoupload is
|
Configs
Key | Type | Default | Description |
---|---|---|---|
uploader.requestMode | string | 'xhr' | See request-mode prop. |
uploader.iframeMode | string | 'postmessage' | See iframe-mode prop. |
uploader.callbackNamespace | string | 'veuiUploadResult' | See callback-namespace prop. |
uploader.headers | Object | - | See headers prop. |
uploader.convertResponse | function(Object): Object | - | See convert-response prop. |
uploader.pickerPosition | string | 'after' | See picker-position prop. |
Icons
Name | Description |
---|---|
upload | Upload files. |
add-image | Upload images. |
clear | Delete. |
success | Upload succeeded. |
failure | Upload failed. |
loading | Uploading. |
file | Uploaded files. |
alert | Validation failure warning. |
preview | Preview images. |
CSS
Name | Type | Default | Description |
---|---|---|---|
--dls-uploader-file-item-width | <length> | - | The width of file list item when type prop is set to file . |