Upload 上传

通过点击或者拖拽上传文件

点击上传

Upload 上传 - 图1

通过 slot 你可以传入自定义的上传按钮类型和文字提示。可通过设置limiton-exceed来限制上传文件的个数和定义超出限制时的行为。可通过设置before-remove来阻止文件移除操作。

  1. <el-upload
  2. class="upload-demo"
  3. action="https://jsonplaceholder.typicode.com/posts/"
  4. :on-preview="handlePreview"
  5. :on-remove="handleRemove"
  6. :before-remove="beforeRemove"
  7. multiple
  8. :limit="3"
  9. :on-exceed="handleExceed"
  10. :file-list="fileList"
  11. >
  12. <el-button size="small" type="primary">点击上传</el-button>
  13. <template #tip>
  14. <div class="el-upload__tip">只能上传 jpg/png 文件,且不超过 500kb</div>
  15. </template>
  16. </el-upload>
  17. <script>
  18. export default {
  19. data() {
  20. return {
  21. 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'}]
  22. };
  23. },
  24. methods: {
  25. handleRemove(file, fileList) {
  26. console.log(file, fileList);
  27. },
  28. handlePreview(file) {
  29. console.log(file);
  30. },
  31. handleExceed(files, fileList) {
  32. this.$message.warning(`当前限制选择 3 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + fileList.length} 个文件`);
  33. },
  34. beforeRemove(file, fileList) {
  35. return this.$confirm(`确定移除 ${ file.name }?`);
  36. }
  37. }
  38. }
  39. </script>

用户头像上传

使用 before-upload 限制用户上传的图片格式和大小。

Upload 上传 - 图2

  1. <el-upload
  2. class="avatar-uploader"
  3. action="https://jsonplaceholder.typicode.com/posts/"
  4. :show-file-list="false"
  5. :on-success="handleAvatarSuccess"
  6. :before-upload="beforeAvatarUpload"
  7. >
  8. <img v-if="imageUrl" :src="imageUrl" class="avatar">
  9. <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  10. </el-upload>
  11. <style>
  12. .avatar-uploader .el-upload {
  13. border: 1px dashed #d9d9d9;
  14. border-radius: 6px;
  15. cursor: pointer;
  16. position: relative;
  17. overflow: hidden;
  18. }
  19. .avatar-uploader .el-upload:hover {
  20. border-color: #409EFF;
  21. }
  22. .avatar-uploader-icon {
  23. font-size: 28px;
  24. color: #8c939d;
  25. width: 178px;
  26. height: 178px;
  27. line-height: 178px;
  28. text-align: center;
  29. }
  30. .avatar {
  31. width: 178px;
  32. height: 178px;
  33. display: block;
  34. }
  35. </style>
  36. <script>
  37. export default {
  38. data() {
  39. return {
  40. imageUrl: ''
  41. };
  42. },
  43. methods: {
  44. handleAvatarSuccess(res, file) {
  45. this.imageUrl = URL.createObjectURL(file.raw);
  46. },
  47. beforeAvatarUpload(file) {
  48. const isJPG = file.type === 'image/jpeg';
  49. const isLt2M = file.size / 1024 / 1024 < 2;
  50. if (!isJPG) {
  51. this.$message.error('上传头像图片只能是 JPG 格式!');
  52. }
  53. if (!isLt2M) {
  54. this.$message.error('上传头像图片大小不能超过 2MB!');
  55. }
  56. return isJPG && isLt2M;
  57. }
  58. }
  59. }
  60. </script>

照片墙

使用 list-type 属性来设置文件列表的样式。

Upload 上传 - 图3

  1. <el-upload
  2. action="https://jsonplaceholder.typicode.com/posts/"
  3. list-type="picture-card"
  4. :on-preview="handlePictureCardPreview"
  5. :on-remove="handleRemove">
  6. <i class="el-icon-plus"></i>
  7. </el-upload>
  8. <el-dialog :visible.sync="dialogVisible">
  9. <img width="100%" :src="dialogImageUrl" alt="">
  10. </el-dialog>
  11. <script>
  12. export default {
  13. data() {
  14. return {
  15. dialogImageUrl: '',
  16. dialogVisible: false
  17. };
  18. },
  19. methods: {
  20. handleRemove(file, fileList) {
  21. console.log(file, fileList);
  22. },
  23. handlePictureCardPreview(file) {
  24. this.dialogImageUrl = file.url;
  25. this.dialogVisible = true;
  26. }
  27. }
  28. }
  29. </script>

文件缩略图

使用 scoped-slot 去设置缩略图模版。

Upload 上传 - 图4

  1. <el-upload
  2. action="#"
  3. list-type="picture-card"
  4. :auto-upload="false">
  5. <template #default>
  6. <i class="el-icon-plus"></i>
  7. </template>
  8. <template #file="{file}">
  9. <div>
  10. <img class="el-upload-list__item-thumbnail" :src="file.url" alt="" />
  11. <span class="el-upload-list__item-actions">
  12. <span
  13. class="el-upload-list__item-preview"
  14. @click="handlePictureCardPreview(file)"
  15. >
  16. <i class="el-icon-zoom-in"></i>
  17. </span>
  18. <span
  19. v-if="!disabled"
  20. class="el-upload-list__item-delete"
  21. @click="handleDownload(file)"
  22. >
  23. <i class="el-icon-download"></i>
  24. </span>
  25. <span
  26. v-if="!disabled"
  27. class="el-upload-list__item-delete"
  28. @click="handleRemove(file)"
  29. >
  30. <i class="el-icon-delete"></i>
  31. </span>
  32. </span>
  33. </div>
  34. </template>
  35. </el-upload>
  36. <el-dialog :visible.sync="dialogVisible">
  37. <img width="100%" :src="dialogImageUrl" alt="">
  38. </el-dialog>
  39. <script>
  40. export default {
  41. data() {
  42. return {
  43. dialogImageUrl: '',
  44. dialogVisible: false,
  45. disabled: false
  46. };
  47. },
  48. methods: {
  49. handleRemove(file) {
  50. console.log(file);
  51. },
  52. handlePictureCardPreview(file) {
  53. this.dialogImageUrl = file.url;
  54. this.dialogVisible = true;
  55. },
  56. handleDownload(file) {
  57. console.log(file);
  58. }
  59. }
  60. }
  61. </script>

图片列表缩略图

Upload 上传 - 图5

  1. <el-upload
  2. class="upload-demo"
  3. action="https://jsonplaceholder.typicode.com/posts/"
  4. :on-preview="handlePreview"
  5. :on-remove="handleRemove"
  6. :file-list="fileList"
  7. list-type="picture">
  8. <el-button size="small" type="primary">点击上传</el-button>
  9. <template #tip>
  10. <div class="el-upload__tip">
  11. 只能上传 jpg/png 文件,且不超过 500kb
  12. </div>
  13. </template>
  14. </el-upload>
  15. <script>
  16. export default {
  17. data() {
  18. return {
  19. 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'}]
  20. };
  21. },
  22. methods: {
  23. handleRemove(file, fileList) {
  24. console.log(file, fileList);
  25. },
  26. handlePreview(file) {
  27. console.log(file);
  28. }
  29. }
  30. }
  31. </script>

上传文件列表控制

通过 on-change 钩子函数来对列表进行控制

Upload 上传 - 图6

  1. <el-upload
  2. class="upload-demo"
  3. action="https://jsonplaceholder.typicode.com/posts/"
  4. :on-change="handleChange"
  5. :file-list="fileList">
  6. <el-button size="small" type="primary">点击上传</el-button>
  7. <template #tip>
  8. <div class="el-upload__tip">
  9. 只能上传 jpg/png 文件,且不超过 500kb
  10. </div>
  11. </template>
  12. </el-upload>
  13. <script>
  14. export default {
  15. data() {
  16. return {
  17. fileList: [{
  18. name: 'food.jpeg',
  19. url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'
  20. }, {
  21. name: 'food2.jpeg',
  22. url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'
  23. }]
  24. };
  25. },
  26. methods: {
  27. handleChange(file, fileList) {
  28. this.fileList = fileList.slice(-3);
  29. }
  30. }
  31. }
  32. </script>

拖拽上传

Upload 上传 - 图7

  1. <el-upload
  2. class="upload-demo"
  3. drag
  4. action="https://jsonplaceholder.typicode.com/posts/"
  5. multiple>
  6. <i class="el-icon-upload"></i>
  7. <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
  8. <template #tip>
  9. <div class="el-upload__tip">
  10. 只能上传 jpg/png 文件,且不超过 500kb
  11. </div>
  12. </template>
  13. </el-upload>

手动上传

Upload 上传 - 图8

  1. <el-upload
  2. class="upload-demo"
  3. ref="upload"
  4. action="https://jsonplaceholder.typicode.com/posts/"
  5. :on-preview="handlePreview"
  6. :on-remove="handleRemove"
  7. :file-list="fileList"
  8. :auto-upload="false">
  9. <template #trigger>
  10. <el-button size="small" type="primary">选取文件</el-button>
  11. </template>
  12. <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button>
  13. <template #tip>
  14. <div class="el-upload__tip">
  15. 只能上传 jpg/png 文件,且不超过 500kb
  16. </div>
  17. </template>
  18. </el-upload>
  19. <script>
  20. export default {
  21. data() {
  22. return {
  23. 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'}]
  24. };
  25. },
  26. methods: {
  27. submitUpload() {
  28. this.$refs.upload.submit();
  29. },
  30. handleRemove(file, fileList) {
  31. console.log(file, fileList);
  32. },
  33. handlePreview(file) {
  34. console.log(file);
  35. }
  36. }
  37. }
  38. </script>

Attribute

参数说明类型可选值默认值
action必选参数,上传的地址string
headers设置上传的请求头部object
multiple是否支持多选文件boolean
data上传时附带的额外参数object
name上传的文件字段名stringfile
with-credentials支持发送 cookie 凭证信息booleanfalse
show-file-list是否显示已上传文件列表booleantrue
drag是否启用拖拽上传booleanfalse
accept接受上传的文件类型(thumbnail-mode 模式下此参数无效)string
on-preview点击文件列表中已上传的文件时的钩子function(file)
on-remove文件列表移除文件时的钩子function(file, fileList)
on-success文件上传成功时的钩子function(response, file, fileList)
on-error文件上传失败时的钩子function(err, file, fileList)
on-progress文件上传时的钩子function(event, file, fileList)
on-change文件状态改变时的钩子,添加文件、上传成功和上传失败时都会被调用function(file, fileList)
before-upload上传文件之前的钩子,参数为上传的文件,若返回 false 或者返回 Promise 且被 reject,则停止上传。function(file)
before-remove删除文件之前的钩子,参数为上传的文件和文件列表,若返回 false 或者返回 Promise 且被 reject,则停止删除。function(file, fileList)
list-type文件列表的类型stringtext/picture/picture-cardtext
auto-upload是否在选取文件后立即进行上传booleantrue
file-list上传的文件列表, 例如: [{name: ‘food.jpg’, url: ‘https://xxx.cdn.com/xxx.jpg'}]array[]
http-request覆盖默认的上传行为,可以自定义上传的实现function
disabled是否禁用booleanfalse
limit最大允许上传个数number
on-exceed文件超出个数限制时的钩子function(files, fileList)-

Slot

name说明
trigger触发文件选择框的内容
tip提示说明文字

Methods

方法名说明参数
clearFiles清空已上传的文件列表(该方法不支持在 before-upload 中调用)
abort取消上传请求( file: fileList 中的 file 对象 )
submit手动上传文件列表