element/examples/docs/zh-CN/upload.md

424 lines
13 KiB
Markdown
Raw Normal View History

2016-07-27 06:15:02 +00:00
<style>
.upload-tip {
color: #8492a6;
font-size: 12px;
margin-top: 7px;
}
.demo-box {
margin-bottom: 24px;
2017-01-09 05:30:42 +00:00
.upload-demo {
width: 360px;
}
2017-02-12 16:44:57 +00:00
.avatar-uploader {
.el-upload {
border: 1px dashed #d9d9d9;
border-radius: 6px;
cursor: pointer;
position: relative;
overflow: hidden;
&:hover {
border-color: #20a0ff;
}
}
.avatar-uploader-icon {
font-size: 28px;
color: #8c939d;
width: 178px;
height: @width;
line-height: @height;
text-align: center;
}
.avatar {
width: 178px;
height: @width;
display: block;
}
}
2016-07-27 06:15:02 +00:00
}
</style>
<script>
export default {
data() {
return {
2017-01-03 03:34:44 +00:00
fileList: [{
name: 'food.jpeg',
url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100',
status: 'finished'
}, {
name: 'food2.jpeg',
url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100',
status: 'finished'
2017-02-12 16:44:57 +00:00
}],
2017-02-20 03:34:59 +00:00
fileList2: [{
name: 'food.jpeg',
url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100',
status: 'finished'
}, {
name: 'food2.jpeg',
url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100',
status: 'finished'
}],
fileList3: [{
name: 'food.jpeg',
url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100',
status: 'finished'
}, {
name: 'food2.jpeg',
url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100',
status: 'finished'
}],
imageUrl: '',
dialogImageUrl: '',
dialogVisible: false
};
},
2016-07-27 06:15:02 +00:00
methods: {
handleRemove(file, fileList) {
console.log(file, fileList);
2016-08-20 10:27:37 +00: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 05:49:09 +00:00
},
2017-02-20 03:34:59 +00:00
handlePictureCardPreview(file) {
this.dialogImageUrl = file.url;
this.dialogVisible = true;
},
2017-02-08 11:05:34 +00:00
submitUpload() {
this.$refs.upload.submit();
2017-02-12 16:44:57 +00:00
},
2017-03-20 04:45:00 +00:00
handleAvatarSuccess(res, file) {
2017-02-12 16:44:57 +00:00
this.imageUrl = URL.createObjectURL(file.raw);
},
beforeAvatarUpload(file) {
const isJPG = file.type === 'image/jpeg';
const isLt2M = file.size / 1024 / 1024 < 2;
if (!isJPG) {
this.$message.error('上传头像图片只能是 JPG 格式!');
}
if (!isLt2M) {
this.$message.error('上传头像图片大小不能超过 2MB!');
}
return isJPG && isLt2M;
2017-02-20 03:34:59 +00:00
},
handleChange(file, fileList) {
this.fileList3 = fileList.slice(-3);
2016-07-27 06:15:02 +00:00
}
}
}
</script>
2016-08-21 07:12:56 +00:00
## Upload 上传
2016-07-27 06:15:02 +00:00
2016-08-21 07:12:56 +00:00
通过点击或者拖拽上传文件
2016-07-27 06:15:02 +00:00
2017-02-12 16:44:57 +00:00
### 点击上传
2016-08-21 07:12:56 +00:00
::: demo 通过 slot 你可以传入自定义的上传按钮类型和文字提示。
2016-07-27 06:15:02 +00:00
```html
2016-09-09 06:11:07 +00:00
<el-upload
2017-01-09 05:30:42 +00:00
class="upload-demo"
2017-03-03 01:02:08 +00:00
action="https://jsonplaceholder.typicode.com/posts/"
2016-09-09 06:11:07 +00:00
:on-preview="handlePreview"
:on-remove="handleRemove"
:file-list="fileList">
2016-07-27 06:15:02 +00:00
<el-button size="small" type="primary">点击上传</el-button>
2017-02-08 11:05:34 +00:00
<div slot="tip" class="el-upload__tip">只能上传jpg/png文件且不超过500kb</div>
2016-07-27 06:15:02 +00:00
</el-upload>
2016-08-20 10:27:37 +00:00
<script>
export default {
data() {
return {
fileList: [{name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}, {name: 'food2.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}]
};
},
2016-08-20 10:27:37 +00:00
methods: {
handleRemove(file, fileList) {
console.log(file, fileList);
},
handlePreview(file) {
console.log(file);
}
}
}
</script>
2016-07-27 06:15:02 +00:00
```
2016-08-21 07:12:56 +00:00
:::
2016-07-27 06:15:02 +00:00
2017-02-12 16:44:57 +00:00
### 用户头像上传
2017-02-20 03:34:59 +00:00
使用 `before-upload` 限制用户上传的图片格式和大小。
2017-02-12 16:44:57 +00:00
::: demo
2017-01-03 03:34:44 +00:00
```html
2017-01-09 05:30:42 +00:00
<el-upload
2017-02-12 16:44:57 +00:00
class="avatar-uploader"
2017-03-03 01:02:08 +00:00
action="https://jsonplaceholder.typicode.com/posts/"
2017-02-12 16:44:57 +00:00
:show-file-list="false"
2017-03-20 04:45:00 +00:00
:on-success="handleAvatarSuccess"
2017-02-12 16:44:57 +00:00
:before-upload="beforeAvatarUpload">
<img v-if="imageUrl" :src="imageUrl" class="avatar">
<i v-else class="el-icon-plus avatar-uploader-icon"></i>
2017-01-09 05:30:42 +00:00
</el-upload>
2017-02-24 06:30:27 +00:00
<style>
.avatar-uploader .el-upload {
border: 1px dashed #d9d9d9;
border-radius: 6px;
cursor: pointer;
position: relative;
overflow: hidden;
}
.avatar-uploader .el-upload:hover {
border-color: #20a0ff;
}
.avatar-uploader-icon {
font-size: 28px;
color: #8c939d;
width: 178px;
height: 178px;
line-height: 178px;
text-align: center;
}
.avatar {
width: 178px;
height: 178px;
display: block;
}
</style>
2017-02-12 16:44:57 +00:00
<script>
export default {
data() {
return {
2017-02-14 16:59:17 +00:00
imageUrl: ''
2017-02-12 16:44:57 +00:00
};
},
methods: {
2017-03-20 04:45:00 +00:00
handleAvatarSuccess(res, file) {
2017-02-14 16:59:17 +00:00
this.imageUrl = URL.createObjectURL(file.raw);
2017-02-12 16:44:57 +00:00
},
2017-02-14 16:59:17 +00:00
beforeAvatarUpload(file) {
const isJPG = file.type === 'image/jpeg';
const isLt2M = file.size / 1024 / 1024 < 2;
if (!isJPG) {
this.$message.error('上传头像图片只能是 JPG 格式!');
}
if (!isLt2M) {
this.$message.error('上传头像图片大小不能超过 2MB!');
}
return isJPG && isLt2M;
2017-02-12 16:44:57 +00:00
}
}
}
</script>
2017-01-03 03:34:44 +00:00
```
:::
2017-02-12 16:44:57 +00:00
### 照片墙
2017-02-20 03:34:59 +00:00
使用 `list-type` 属性来设置文件列表的样式。
2017-02-08 11:05:34 +00:00
::: demo
```html
<el-upload
2017-03-03 01:02:08 +00:00
action="https://jsonplaceholder.typicode.com/posts/"
2017-02-12 16:44:57 +00:00
list-type="picture-card"
2017-02-20 03:34:59 +00:00
:on-preview="handlePictureCardPreview"
2017-02-14 16:59:17 +00:00
:on-remove="handleRemove">
2017-02-12 16:44:57 +00:00
<i class="el-icon-plus"></i>
2017-02-08 11:05:34 +00:00
</el-upload>
2017-02-20 03:34:59 +00:00
<el-dialog v-model="dialogVisible" size="tiny">
<img width="100%" :src="dialogImageUrl" alt="">
</el-dialog>
2017-02-08 11:05:34 +00:00
<script>
export default {
2017-02-20 03:34:59 +00:00
data() {
return {
dialogImageUrl: '',
dialogVisible: false
};
},
2017-02-08 11:05:34 +00:00
methods: {
handleRemove(file, fileList) {
console.log(file, fileList);
},
2017-02-20 03:34:59 +00:00
handlePictureCardPreview(file) {
this.dialogImageUrl = file.url;
this.dialogVisible = true;
2017-02-08 11:05:34 +00:00
}
}
}
</script>
```
:::
2017-02-12 16:44:57 +00:00
### 图片列表缩略图
2016-07-27 06:15:02 +00:00
2017-02-12 16:44:57 +00:00
::: demo
2016-07-27 06:15:02 +00:00
```html
2016-08-20 10:27:37 +00:00
<el-upload
2017-02-12 16:44:57 +00:00
class="upload-demo"
2017-03-03 01:02:08 +00:00
action="https://jsonplaceholder.typicode.com/posts/"
2016-08-20 10:27:37 +00:00
:on-preview="handlePreview"
2016-08-21 07:12:56 +00:00
:on-remove="handleRemove"
2017-02-20 03:34:59 +00:00
:file-list="fileList2"
2017-02-12 16:44:57 +00:00
list-type="picture">
<el-button size="small" type="primary">点击上传</el-button>
<div slot="tip" class="el-upload__tip">只能上传jpg/png文件且不超过500kb</div>
2016-07-27 06:15:02 +00:00
</el-upload>
2016-08-20 10:27:37 +00:00
<script>
export default {
data() {
return {
2017-02-20 03:34:59 +00:00
fileList2: [{name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}, {name: 'food2.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}]
};
},
2016-08-20 10:27:37 +00:00
methods: {
handleRemove(file, fileList) {
console.log(file, fileList);
},
handlePreview(file) {
console.log(file);
}
}
}
</script>
2016-07-27 06:15:02 +00:00
```
2016-08-21 07:12:56 +00:00
:::
2016-07-27 06:15:02 +00:00
2017-02-20 03:34:59 +00:00
### 上传文件列表控制
通过 `on-change` 钩子函数来对列表进行控制
::: demo
```html
<el-upload
class="upload-demo"
2017-03-03 01:02:08 +00:00
action="https://jsonplaceholder.typicode.com/posts/"
2017-02-20 03:34:59 +00:00
:on-change="handleChange"
:file-list="fileList3">
<el-button size="small" type="primary">点击上传</el-button>
<div slot="tip" class="el-upload__tip">只能上传jpg/png文件且不超过500kb</div>
</el-upload>
<script>
export default {
data() {
return {
fileList3: [{
name: 'food.jpeg',
url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100',
status: 'finished'
}, {
name: 'food2.jpeg',
url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100',
status: 'finished'
}]
};
},
methods: {
handleChange(file, fileList) {
this.fileList3 = fileList.slice(-3);
}
}
}
</script>
```
:::
2017-02-12 16:44:57 +00:00
### 拖拽上传
2016-07-27 06:15:02 +00:00
2017-02-12 16:44:57 +00:00
::: demo
2016-07-27 06:15:02 +00:00
```html
2016-08-20 10:27:37 +00:00
<el-upload
2017-02-12 16:44:57 +00:00
class="upload-demo"
drag
2017-03-03 01:02:08 +00:00
action="https://jsonplaceholder.typicode.com/posts/"
2017-03-20 04:45:00 +00:00
multiple>
2016-09-06 17:15:39 +00:00
<i class="el-icon-upload"></i>
2017-02-12 16:44:57 +00:00
<div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
2016-07-27 06:15:02 +00:00
<div class="el-upload__tip" slot="tip">只能上传jpg/png文件且不超过500kb</div>
</el-upload>
2017-02-12 16:44:57 +00:00
```
:::
### 手动上传
::: demo
```html
<el-upload
class="upload-demo"
ref="upload"
2017-03-03 01:02:08 +00:00
action="https://jsonplaceholder.typicode.com/posts/"
2017-02-12 16:44:57 +00:00
:on-preview="handlePreview"
:on-remove="handleRemove"
:file-list="fileList"
:auto-upload="false">
<el-button slot="trigger" size="small" type="primary">选取文件</el-button>
<el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button>
<div slot="tip" class="el-upload__tip">只能上传jpg/png文件且不超过500kb</div>
</el-upload>
2016-08-20 10:27:37 +00:00
<script>
export default {
data() {
return {
fileList: [{name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}, {name: 'food2.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}]
};
},
2016-08-20 10:27:37 +00:00
methods: {
submitUpload() {
this.$refs.upload.submit();
},
2016-08-20 10:27:37 +00:00
handleRemove(file, fileList) {
console.log(file, fileList);
},
handlePreview(file) {
console.log(file);
}
}
}
</script>
2016-07-27 06:15:02 +00:00
```
2017-02-12 16:44:57 +00:00
:::
2016-08-21 07:12:56 +00:00
2016-12-15 07:15:46 +00:00
### Attribute
2016-07-27 06:15:02 +00:00
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
|---------- |-------------- |---------- |-------------------------------- |-------- |
2016-09-02 05:56:47 +00:00
| action | 必选参数, 上传的地址 | string | — | — |
2016-08-21 07:12:56 +00:00
| headers | 可选参数, 设置上传的请求头部 | object | — | — |
| multiple | 可选参数, 是否支持多选文件 | boolean | — | — |
| data | 可选参数, 上传时附带的额外参数 | object | — | — |
| name | 可选参数, 上传的文件字段名 | string | — | file |
2016-09-09 06:11:07 +00:00
| with-credentials | 支持发送 cookie 凭证信息 | boolean | — | false |
2017-02-12 16:44:57 +00:00
| show-file-list | 是否显示已上传文件列表 | boolean | — | true |
| drag | 是否启用拖拽上传 | boolean | — | false |
2016-10-31 08:49:42 +00:00
| accept | 可选参数, 接受上传的[文件类型](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-accept)thumbnail-mode 模式下此参数无效)| string | — | — |
| on-preview | 可选参数, 点击已上传的文件链接时的钩子, 可以通过 file.response 拿到服务端返回数据 | function(file) | — | — |
2016-08-21 07:12:56 +00:00
| on-remove | 可选参数, 文件列表移除文件时的钩子 | function(file, fileList) | — | — |
| on-success | 可选参数, 文件上传成功时的钩子 | function(response, file, fileList) | — | — |
2017-02-14 16:35:34 +00:00
| on-error | 可选参数, 文件上传失败时的钩子 | function(err, file, fileList) | — | — |
| on-progress | 可选参数, 文件上传时的钩子 | function(event, file, fileList) | — | — |
2017-04-25 13:56:46 +00:00
| on-change | 可选参数, 文件状态改变时的钩子,添加文件、上传成功和上传失败时都会被调用 | function(file, fileList) | — | — |
2017-05-10 12:44:49 +00:00
| before-upload | 可选参数, 上传文件之前的钩子,参数为上传的文件,若返回 false 或者返回 Promise 且被 reject则停止上传。 | function(file) | — | — |
2017-02-12 16:44:57 +00:00
| list-type | 文件列表的类型 | string | text/picture/picture-card | text |
| auto-upload | 是否在选取文件后立即进行上传 | boolean | — | true |
2017-02-15 13:16:30 +00:00
| file-list | 上传的文件列表, 例如: [{name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}] | array | — | [] |
2017-03-09 06:30:00 +00:00
| http-request | 覆盖默认的上传行为,可以自定义上传的实现 | function | — | — |
2017-04-21 03:57:30 +00:00
| disabled | 是否禁用 | boolean | — | false |
2016-11-08 11:59:23 +00:00
2016-12-15 07:15:46 +00:00
### Methods
2016-11-08 11:59:23 +00:00
| 方法名 | 说明 | 参数 |
2016-12-29 14:07:50 +00:00
|---------- |-------------- | -- |
| clearFiles | 清空已上传的文件列表(该方法不支持在 before-upload 中调用) | — |
2017-05-02 04:46:57 +00:00
| abort | 取消上传请求 | file: fileList 中的 file 对象 |