vant/src/uploader/README.md
2020-01-29 10:11:09 +08:00

169 lines
3.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Uploader
### Install
```js
import Vue from 'vue';
import { Uploader } from 'vant';
Vue.use(Uploader);
```
## Usage
### Basic Usage
```html
<van-uploader :after-read="afterRead" />
```
```js
export default {
methods: {
afterRead(file) {
console.log(file)
}
}
};
```
### Preview File
```html
<van-uploader v-model="fileList" multiple />
```
```js
export default {
data() {
return {
fileList: [
{ url: 'https://img.yzcdn.cn/vant/leaf.jpg' }
]
}
}
};
```
### Max Count
```html
<van-uploader
v-model="fileList"
multiple
:max-count="2"
/>
```
```js
export default {
data() {
return {
fileList: []
}
}
};
```
### Upload Style
```html
<van-uploader>
<van-button icon="photo" type="primary">Upload Image</van-button>
</van-uploader>
```
### Before Read
```html
<van-uploader :before-read="beforeRead" />
```
```js
import { Toast } from 'vant';
export default {
methods: {
beforeRead(file) {
if (file.type !== 'image/jpeg') {
Toast('Please upload an image in jpg format');
return false;
}
return true;
},
asyncBeforeRead(file) {
return new Promise((resolve, reject) => {
if (file.type !== 'image/jpeg') {
Toast('Please upload an image in jpg format');
reject();
} else {
resolve();
}
});
}
}
}
```
## API
### Props
| Attribute | Description | Type | Default |
|------|------|------|------|
| name `v2.0.3` | Input name | *number \| string* | - |
| accept | Accepted file type | *string* | `image/*` |
| preview-size | Size of preview image | *number \| string* | `80px` |
| preview-image `v2.1.5` | Whether to show image preview | *boolean* | `true` |
| preview-full-image | Whethe to show full screen image preview when click image | *boolean* | `true` |
| multiple | Whether to enable multiple selection pictures | *boolean* | `false` |
| disabled | Whether to disabled the upload | *boolean* | `false` |
| deletable `v2.2.12` | Whether to show delete icon | *boolean* | `true` |
| capture | Capturecan 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 reading the file, can return Promise | *Function* | - |
| max-size | Max size of file | *number* | - |
| max-count | Max count of image | *number* | - |
| result-type `v2.2.7` | Type of file read result, can be set to `file` `text` | *string* | `dataUrl` |
| upload-text | Upload text | *string* | - |
| image-fit `v2.1.5` | Preview image fit mode | *string* | `cover` |
### Events
| Event | Description | Arguments |
|------|------|------|
| oversize | Triggered when file size over limit | Same as after-read |
| click-preview | Triggered when click preview image | Same as after-read |
| close-preview | Triggered when close full screen image preview | - |
| delete | Triggered when delete preview file | Same as after-read |
### Slots
| Name | Description |
|------|------|
| default | Custom icon |
### Parematers of before-read、after-read、before-delete
| Attribute | Description | Type |
|------|------|------|
| file | File object | *object* |
| detail | Detail info, contains name and index | *object* |
### ResultType
| Value | Description |
|------|------|
| file | Result contains File object |
| text | Result contains File object and text content |
| dataUrl | Result contains File object and base64 content |
### Methods
Use [ref](https://vuejs.org/v2/api/#ref) to get Uploader instance and call instance methods
| Name | Description | Attribute | Return value |
|------|------|------|------|
| closeImagePreview | Close full screen image preview | - | - |