Skip to content

Uploader - Vant 4

Uploader

Intro

Used to upload a local image or file to the server and display a preview image and upload progress during the upload process. The Uploader component does not currently contain the interface logic for uploading files to the server, this step needs to be implemented by the user.

Install

Register component globally via app.use, refer to Component Registration for more registration ways.

js
import { createApp } from'vue'; import { Uploader } from'vant'; const app = createApp(); app.use(Uploader);

Usage

Basic Usage

html
js
exportdefault { setup() { constafterRead = (file) => { console.log(file); }; return { afterRead, }; }, };

Preview File

html
js
import { ref } from'vue'; exportdefault { setup() { const fileList = ref([ { url: 'https://fastly.jsdelivr.net/npm/@vant/assets/leaf.jpeg' }, { url: 'https://cloud-image', isImage: true }, ]); return { fileList, }; }, };

Upload Status

html
js
import { ref } from'vue'; exportdefault { setup() { const fileList = ref([ { url: 'https://fastly.jsdelivr.net/npm/@vant/assets/leaf.jpeg', status: 'uploading', message: 'Uploading...', }, { url: 'https://fastly.jsdelivr.net/npm/@vant/assets/tree.jpeg', status: 'failed', message: 'Failed', }, ]); constafterRead = (file) => { file.status = 'uploading'; file.message = 'Uploading...'; setTimeout(() => { file.status = 'failed'; file.message = 'Failed'; }, 1000); }; return { fileList, afterRead, }; }, };

Max Count

html
js
import { ref } from'vue'; exportdefault { setup() { const fileList = ref([]); return { fileList, }; }, };

Max Size

html
js
import { showToast } from'vant'; exportdefault { setup() { constonOversize = (file) => { console.log(file); showToast('File size cannot exceed 500kb'); }; return { onOversize, }; }, };

If you need to make different size limits for different types of files, you can pass a function to the max-size props.

html
js
exportdefault { setup() { constisOverSize = (file) => { const maxSize = file.type === 'image/jpeg' ? 500 * 1024 : 1000 * 1024; return file.size >= maxSize; }; return { isOverSize, }; }, };

Custom Upload Area

html

Preview Cover

html

Preview Size

Using preview-size prop to custom the size of preview image.

html

You can set the width and height separately.

html

Before Read

html
js
import { showToast } from'vant'; exportdefault { setup() { // ���ز���ֵconstbeforeRead = (file) => { if (file.type !== 'image/jpeg') { showToast('Please upload an image in jpg format'); returnfalse; } returntrue; }; // ���� PromiseconstasyncBeforeRead = (file) => newPromise((resolve, reject) => { if (file.type !== 'image/jpeg') { showToast('Please upload an image in jpg format'); reject(); } else { const img = newFile(['foo'], 'bar.jpg', { type: 'image/jpeg', }); resolve(img); } }); return { beforeRead, asyncBeforeRead, }; }, };

Disable Uploader

Use disabled prop to disable uploader.

html

Customize Single Preview Image Style

html
js
import { ref } from'vue'; import { showToast } from'vant'; exportdefault { setup() { const fileList = ref([ { url: 'https://fastly.jsdelivr.net/npm/@vant/assets/sand.jpeg', deletable: true, beforeDelete: () => { showToast( 'Customize the events and styles of a single preview image', ); }, }, { url: 'https://fastly.jsdelivr.net/npm/@vant/assets/tree.jpeg', imageFit: 'contain', }, ]); return { fileList }; }, };

Enable Reupload

html
ts
import { ref } from'vue'; exportdefault { setup() { const fileList = ref([ { url: 'https://fastly.jsdelivr.net/npm/@vant/assets/leaf.jpeg' }, ]); return { fileList }; }, };

API

Props

AttributeDescriptionTypeDefault
v-modelList of uploaded filesFileListItem[]-
acceptAccepted file typestringimage/*

| name | Input name, usually a unique string or number | number | string | - | | preview-size | Size of preview image | number | string | Array | 80px | | preview-image | Whether to show image preview | boolean | true | | preview-full-image | Whether to show full screen image preview when image is clicked | boolean | true | | preview-options | Options of full screen image preview, see ImagePreview | object | - |

| multiple | Whether to enable multiple selection pictures | boolean | false | | disabled | Whether to disabled the upload | boolean | false | | readonly | Whether to make upload area readonly | boolean | false | | deletable | Whether to show delete icon | boolean | true | | reupload v4.4.0 | Whether to enable reupload, if enabled, the image preview will be disabled | boolean | false | | show-upload | Whether to show upload area | boolean | true | | lazy-load | Whether to enable lazy load, should register Lazyload component | boolean | false |

| capture | Capture, can be set to camera | string | - | | after-read | Hook after reading the file | Function | - | | before-read | Hook before reading the file, return false to stop reading the file, can return Promise | Function | - | | before-delete | Hook before delete the file, return false to stop deleting the file, can return Promise | Function | - | | max-size | Max size of file | number | string | (file: File) => boolean | Infinity | | max-count | Max count of image | number | string | Infinity | | result-type | Type of file read result, can be set to file``text | string | dataUrl | | upload-text | Upload text | string | - | | image-fit | Preview image fit mode | string | cover | | upload-icon | Upload icon | string | photograph |

Tips: accept, capture and multiple are the attributes of the native input tag, there may be some compatibility issues under different systems and WebView.

Events

EventDescriptionArguments
oversizeEmitted when file size over limitSame as after-read
click-uploadEmitted when click upload areaevent: MouseEvent
click-previewEmitted when preview image is clickedSame as after-read
click-reuploadEmitted when reupload image is clickedSame as after-read
close-previewEmitted when the full screen image preview is closed-
deleteEmitted when preview file is deletedSame as after-read

Slots

NameDescriptionSlotProps
defaultCustom upload area-
preview-deleteCustom delete icon-
preview-coverCustom content that covers the image previewitem: FileListItem

Parameters of before-read��before-delete

AttributeDescriptionType
fileFile objectobject
detailDetail info, contains name and indexobject

Parameters of after-read

AttributeDescriptionType
fileContains File object*UploaderFileListItem
detailDetail info, contains name and indexobject

ResultType

ValueDescription
fileResult contains File object
textResult contains File object and text content
dataUrlResult contains File object and base64 content

Methods

Use ref to get Uploader instance and call instance methods.

NameDescriptionAttributeReturn value
closeImagePreviewClose full screen image preview--
chooseFileTrigger choosing files, works with the user action context only because of browser security--
reuploadFile 4.9.3Trigger choosing files, choosing a new file will overwrite the previously uploaded file, works with the user action context only because of browser securityindex: number-

Types

The component exports the following type definitions:

ts
importtype { UploaderProps, UploaderInstance, UploaderResultType, UploaderFileListItem, UploaderBeforeRead, UploaderAfterRead, } from'vant';

UploaderInstance is the type of component instance:

ts
import { ref } from'vue'; importtype { UploaderInstance } from'vant'; const uploaderRef = ref<UploaderInstance>(); uploaderRef.value?.chooseFile();

Theming

CSS Variables

The component provides the following CSS variables, which can be used to customize styles. Please refer to ConfigProvider component.

NameDefault ValueDescription
--van-uploader-size80px-
--van-uploader-icon-size24px-
--van-uploader-icon-colorvar(--van-gray-4)-
--van-uploader-text-colorvar(--van-text-color-2)-
--van-uploader-text-font-sizevar(--van-font-size-sm)-
--van-uploader-upload-backgroundvar(--van-gray-1)-
--van-uploader-upload-active-colorvar(--van-active-color)-
--van-uploader-delete-colorvar(--van-white)-
--van-uploader-delete-icon-size14px-
--van-uploader-delete-backgroundrgba(0, 0, 0, 0.7)-
--van-uploader-file-backgroundvar(--van-background)-
--van-uploader-file-icon-size20px-
--van-uploader-file-icon-colorvar(--van-gray-7)-
--van-uploader-file-name-padding0 var(--van-padding-base)-
--van-uploader-file-name-margin-topvar(--van-padding-xs)-
--van-uploader-file-name-font-sizevar(--van-font-size-sm)-
--van-uploader-file-name-text-colorvar(--van-gray-7)-
--van-uploader-mask-text-colorvar(--van-white)-
--van-uploader-mask-backgroundfade(var(--van-gray-8), 88%)-
--van-uploader-mask-icon-size22px-
--van-uploader-mask-message-font-sizevar(--van-font-size-sm)-
--van-uploader-mask-message-line-heightvar(--van-line-height-xs)-
--van-uploader-loading-icon-size22px-
--van-uploader-loading-icon-colorvar(--van-white)-
--van-uploader-disabled-opacityvar(--van-disabled-opacity)-
--van-uploader-border-radius0px-

FAQ

How do I know if the user has granted camera permission?

When uploading an image, if the user has not granted camera permission to the current app, the Uploader component will not work.

You can determine if camera permission has been granted by using the getUserMedia method provided by the browser (please note that the getUserMedia method cannot be used in iOS 10).

Here is a simplified example:

ts
navigator.mediaDevices .getUserMedia({ video: true }) .then((stream) => { console.log(stream); }) .catch((err) => { console.log(err); });

Enterprise-level mobile solution based on Vant