2016-07-27 14:15:02 +08:00
|
|
|
|
<style>
|
|
|
|
|
.upload-tip {
|
|
|
|
|
color: #8492a6;
|
|
|
|
|
font-size: 12px;
|
|
|
|
|
margin-top: 7px;
|
|
|
|
|
}
|
|
|
|
|
.demo-box {
|
|
|
|
|
margin-bottom: 24px;
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
<script>
|
|
|
|
|
export default {
|
|
|
|
|
methods: {
|
|
|
|
|
handleChange(file, fileList, event) {
|
|
|
|
|
console.log(file, fileList, event);
|
|
|
|
|
},
|
|
|
|
|
handleRemove(file, fileList) {
|
|
|
|
|
console.log(file, fileList);
|
2016-08-20 18:27:37 +08:00
|
|
|
|
},
|
|
|
|
|
beforeUpload(file) {
|
|
|
|
|
if (file.size > 40000000) {
|
|
|
|
|
console.warn(file.name + ' is too large!');
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
handlePreview(file) {
|
|
|
|
|
console.log(file);
|
2016-09-01 13:49:09 +08:00
|
|
|
|
},
|
|
|
|
|
handleSuccess(file, fileList) {
|
|
|
|
|
console.log(file, fileList);
|
|
|
|
|
},
|
|
|
|
|
handleError(err, file, fileList) {
|
|
|
|
|
console.log(err);
|
2016-07-27 14:15:02 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
2016-08-21 15:12:56 +08:00
|
|
|
|
## Upload 上传
|
2016-07-27 14:15:02 +08:00
|
|
|
|
|
2016-08-21 15:12:56 +08:00
|
|
|
|
通过点击或者拖拽上传文件
|
2016-07-27 14:15:02 +08:00
|
|
|
|
|
2016-08-21 15:12:56 +08:00
|
|
|
|
### 点击上传多个文件
|
|
|
|
|
|
|
|
|
|
::: demo 通过 slot 你可以传入自定义的上传按钮类型和文字提示。
|
2016-07-27 14:15:02 +08:00
|
|
|
|
```html
|
2016-09-09 14:11:07 +08:00
|
|
|
|
<el-upload
|
2016-09-21 18:45:29 +08:00
|
|
|
|
action="http://jsonplaceholder.typicode.com/"
|
2016-09-09 14:11:07 +08:00
|
|
|
|
:on-preview="handlePreview"
|
|
|
|
|
:on-remove="handleRemove">
|
2016-07-27 14:15:02 +08:00
|
|
|
|
<el-button size="small" type="primary">点击上传</el-button>
|
|
|
|
|
<div class="el-upload__tip" slot="tip">只能上传jpg/png文件,且不超过500kb</div>
|
|
|
|
|
</el-upload>
|
2016-08-20 18:27:37 +08:00
|
|
|
|
<script>
|
|
|
|
|
export default {
|
|
|
|
|
methods: {
|
|
|
|
|
handleRemove(file, fileList) {
|
|
|
|
|
console.log(file, fileList);
|
|
|
|
|
},
|
|
|
|
|
handlePreview(file) {
|
|
|
|
|
console.log(file);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</script>
|
2016-07-27 14:15:02 +08:00
|
|
|
|
```
|
2016-08-21 15:12:56 +08:00
|
|
|
|
:::
|
2016-07-27 14:15:02 +08:00
|
|
|
|
|
2016-08-21 15:12:56 +08:00
|
|
|
|
### 拖拽上传
|
2016-07-27 14:15:02 +08:00
|
|
|
|
|
2016-08-21 15:12:56 +08:00
|
|
|
|
可将文件拖入指定区域进行上传。
|
2016-07-27 14:15:02 +08:00
|
|
|
|
|
2016-08-21 15:12:56 +08:00
|
|
|
|
::: demo 将 `type` 属性指定为 'drag' 可以将上传控件变为支持拖拽的形式,并且你可以通过 `multiple` 属性来控制是否支持多选,`on-preview` 和 `on-remove` 是一个钩子函数,分别在点击上传后的文件链接和点击移除上传后的文件后被调用。
|
2016-07-27 14:15:02 +08:00
|
|
|
|
```html
|
2016-08-20 18:27:37 +08:00
|
|
|
|
<el-upload
|
2016-09-21 18:45:29 +08:00
|
|
|
|
action="http://jsonplaceholder.typicode.com/"
|
2016-08-20 18:27:37 +08:00
|
|
|
|
type="drag"
|
|
|
|
|
:multiple="true"
|
|
|
|
|
:on-preview="handlePreview"
|
2016-08-21 15:12:56 +08:00
|
|
|
|
:on-remove="handleRemove"
|
2016-09-01 13:49:09 +08:00
|
|
|
|
:on-success="handleSuccess"
|
|
|
|
|
:on-error="handleError"
|
2016-08-21 15:12:56 +08:00
|
|
|
|
>
|
2016-09-07 01:15:39 +08:00
|
|
|
|
<i class="el-icon-upload"></i>
|
2016-08-21 15:12:56 +08:00
|
|
|
|
<div class="el-dragger__text">将文件拖到此处,或<em>点击上传</em></div>
|
2016-07-27 14:15:02 +08:00
|
|
|
|
<div class="el-upload__tip" slot="tip">只能上传jpg/png文件,且不超过500kb</div>
|
|
|
|
|
</el-upload>
|
2016-08-20 18:27:37 +08:00
|
|
|
|
<script>
|
|
|
|
|
export default {
|
|
|
|
|
methods: {
|
|
|
|
|
handleRemove(file, fileList) {
|
|
|
|
|
console.log(file, fileList);
|
|
|
|
|
},
|
|
|
|
|
handlePreview(file) {
|
|
|
|
|
console.log(file);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</script>
|
2016-07-27 14:15:02 +08:00
|
|
|
|
```
|
2016-08-21 15:12:56 +08:00
|
|
|
|
:::
|
2016-07-27 14:15:02 +08:00
|
|
|
|
|
2016-08-21 15:12:56 +08:00
|
|
|
|
### 上传单个图片
|
2016-07-27 14:15:02 +08:00
|
|
|
|
|
2016-08-21 15:12:56 +08:00
|
|
|
|
专门针对图片类型文件的上传,上传后在原位置显示缩略图。
|
2016-07-27 14:15:02 +08:00
|
|
|
|
|
2016-08-21 15:12:56 +08:00
|
|
|
|
::: demo `thumbnail-mode` 属性允许你将上传组件强制只允许图片上传,并支持展示上传文件的缩略图。
|
2016-07-27 14:15:02 +08:00
|
|
|
|
```html
|
2016-08-20 18:27:37 +08:00
|
|
|
|
<el-upload
|
2016-09-21 18:45:29 +08:00
|
|
|
|
action="http://jsonplaceholder.typicode.com/"
|
2016-08-20 18:27:37 +08:00
|
|
|
|
type="drag"
|
|
|
|
|
:thumbnail-mode="true"
|
|
|
|
|
:on-preview="handlePreview"
|
|
|
|
|
:on-remove="handleRemove"
|
|
|
|
|
>
|
2016-09-07 01:15:39 +08:00
|
|
|
|
<i class="el-icon-upload"></i>
|
2016-08-21 15:12:56 +08:00
|
|
|
|
<div class="el-dragger__text">将文件拖到此处,或<em>点击上传</em></div>
|
2016-07-27 14:15:02 +08:00
|
|
|
|
<div class="el-upload__tip" slot="tip">只能上传jpg/png文件,且不超过500kb</div>
|
|
|
|
|
</el-upload>
|
2016-08-20 18:27:37 +08:00
|
|
|
|
<script>
|
|
|
|
|
export default {
|
|
|
|
|
methods: {
|
|
|
|
|
handleRemove(file, fileList) {
|
|
|
|
|
console.log(file, fileList);
|
|
|
|
|
},
|
|
|
|
|
handlePreview(file) {
|
|
|
|
|
console.log(file);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</script>
|
2016-07-27 14:15:02 +08:00
|
|
|
|
```
|
2016-08-21 15:12:56 +08:00
|
|
|
|
:::
|
|
|
|
|
|
|
|
|
|
### Upload Attribute
|
2016-07-27 14:15:02 +08:00
|
|
|
|
|
|
|
|
|
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
|
|
|
|
|
|---------- |-------------- |---------- |-------------------------------- |-------- |
|
2016-09-02 13:56:47 +08:00
|
|
|
|
| action | 必选参数, 上传的地址 | string | — | — |
|
2016-08-21 15:12:56 +08:00
|
|
|
|
| headers | 可选参数, 设置上传的请求头部 | object | — | — |
|
|
|
|
|
| multiple | 可选参数, 是否支持多选文件 | boolean | — | — |
|
|
|
|
|
| file | 可选参数, 上传的文件字段名 | string | — | file |
|
2016-09-09 14:11:07 +08:00
|
|
|
|
| with-credentials | 支持发送 cookie 凭证信息 | boolean | — | false |
|
2016-08-21 15:12:56 +08:00
|
|
|
|
| show-uploadList | 是否显示已上传文件列表 | boolean | — | true |
|
2016-07-27 14:15:02 +08:00
|
|
|
|
| type | 上传控件类型 | string | select,drag | select |
|
2016-08-21 15:12:56 +08:00
|
|
|
|
| accept | 可选参数, 接受上传的[文件类型](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-accept), 拖拽文件上传时不受此参数影响 | string | — | — |
|
|
|
|
|
| on-preview | 可选参数, 点击已上传的文件链接时的钩子 | function(file) | — | — |
|
|
|
|
|
| on-remove | 可选参数, 文件列表移除文件时的钩子 | function(file, fileList) | — | — |
|
2016-09-01 13:49:09 +08:00
|
|
|
|
| on-success | 可选参数, 文件上传成功时的钩子 | function(file, fileList) | — | — |
|
|
|
|
|
| on-error | 可选参数, 文件上传失败时的钩子 | function(err, file, fileList) | — | — |
|
2016-08-21 15:12:56 +08:00
|
|
|
|
| before-upload | 可选参数, 上传文件之前的钩子,参数为上传的文件,若返回 false 或者 Promise 则停止上传。 | function(file) | — | — |
|
|
|
|
|
| thumbnail-mode | 是否设置为图片模式,该模式下会显示图片缩略图 | boolean | — | false |
|