May 07, 2021 Vant
import Vue from 'vue';
import { Uploader } from 'vant';
Vue.use(Uploader);
When the file is uploaded, theafter-read callback function is triggered to get the corresponding file object
<van-uploader :after-read="afterRead" />
export default {
methods: {
afterRead(file) {
// 此时可以自行将文件上传至服务器
console.log(file);
}
}
};
V-model allows you to bind a list of images that have been uploaded and show a preview of the list of pictures
<van-uploader v-model="fileList" multiple />
export default {
data() {
return {
fileList: [
{ url: 'https://img.yzcdn.cn/vant/leaf.jpg' },
// Uploader 根据文件后缀来判断是否为图片文件
// 如果图片 URL 中不包含类型信息,可以添加 isImage 标记来声明
{ url: 'https://cloud-image', isImage: true }
]
}
}
};
The max-count property allows you to limit the number of uploaded files, and when the number of uploads reaches the limit, the upload area is automatically hidden
<van-uploader
v-model="fileList"
multiple
:max-count="2"
/>
export default {
data() {
return {
fileList: []
}
}
};
The slot allows you to customize the style of the upload area
<van-uploader>
<van-button icon="photo" type="primary">上传图片</van-button>
</van-uploader>
By passing in the beforeRead function, you can verify before uploading, return true to indicate check pass, return to false to indicate that the check failed. Supports returning Promise for asynchronous validation
<van-uploader :before-read="beforeRead" />
import { Toast } from 'vant';
export default {
methods: {
// 返回布尔值
beforeRead(file) {
if (file.type !== 'image/jpeg') {
Toast('请上传 jpg 格式图片');
return false;
}
return true;
},
// 返回 Promise
asyncBeforeRead(file) {
return new Promise((resolve, reject) => {
if (file.type !== 'image/jpeg') {
Toast('请上传 jpg 格式图片');
reject();
} else {
resolve();
}
});
}
}
}
Parameters | Description | Type | The default |
---|---|---|---|
accept | The type of file that is allowed to be uploaded, detailing | string |
image/*
|
name
v2.0.3
|
Identifier, which can be obtained in the second argument of the callback function | number | string | - |
preview-size |
Preview the size of the drawing and the upload area in
px
|
number | string |
80px
|
preview-image | Whether to display the preview after the upload is complete | boolean |
true
|
preview-full-image
v2.1.5
|
Show a full-screen picture preview after clicking on the preview | boolean |
true
|
multiple | Whether to turn on multiple selection of pictures, some Android models do not support | boolean |
false
|
disabled | Whether to disable file upload | boolean |
false
|
deletable
v2.2.12
|
Whether to display the delete button | boolean |
true
|
capture |
Picture selection mode, optional value is
camera
(directly up camera)
|
string | - |
after-read | The callback function after the file read is complete | Function | - |
before-read |
Callback function before file read, return
false
to terminate file read,
Back to Promise is
Promise
|
Function | - |
before-delete |
Callback function before file deletion, return
false
to terminate file read,
Back to Promise is
Promise
|
Function | - |
max-size |
File size limit in
byte
|
number | string | - |
max-count | There is a limit to the number of file uploads | number | string | - |
result-type
v2.2.7
|
The file read result type, with an optional
file
text
|
string |
dataUrl
|
upload-text | Upload a regional text prompt | string | - |
image-fit
v2.1.5
|
Preview the graph crop mode, with optional values available in the Image component | string |
cover
|
The name of the event | Description | Callback parameters |
---|---|---|
oversize | Triggered when the file size exceeds the limit |
Same
after-read
|
click-preview | Triggered when you click on the preview |
Same
after-read
|
close-preview | Trigger when you turn off full-screen picture previews | - |
delete | Triggered when the file preview is deleted |
Same
after-read
|
Name | Description |
---|---|
default | Customize the upload area |
The following callback parameters are passed when before-read, after-read, before-delete are executed:
The name of the argument | Description | Type |
---|---|---|
file | The file object after the file is parsed | object |
detail | Additional information, including name and index fields | object |
The result-type field represents the type of file read result, and file type is recommended to avoid Caton when uploading large files.
Value | Describe |
---|---|
file | The result contains only file objects |
text | The result contains the File object, as well as the text content of the file |
dataUrl | The result contains the File object, as well as the base64 encoding for the file |
Ref allows you to get to the Uploader instance and call the instance method, as detailed in The Component Instance Method
The method name | Description | Parameters | Returns a value |
---|---|---|---|
closeImagePreview | Turn off full-screen picture previews | - | - |