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

7.7 KiB
Raw Blame History

Upload 上传

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

点击上传多个文件

::: demo 通过 slot 你可以传入自定义的上传按钮类型和文字提示。

<el-upload
  class="upload-demo"
  action="//jsonplaceholder.typicode.com/posts/"
  :on-preview="handlePreview"
  :on-remove="handleRemove"
  :file-list="fileList">
  <el-button size="small" type="primary">点击上传</el-button>
  <div class="el-upload__tip" slot="tip">只能上传jpg/png文件且不超过500kb</div>
</el-upload>
<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'}]
      };
    },
    methods: {
      handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      handlePreview(file) {
        console.log(file);
      }
    }
  }
</script>

:::

::: demo 通过 slot 你可以传入自定义的上传按钮类型和文字提示。

<el-upload
  class="upload-demo"
  dragger
  action="//jsonplaceholder.typicode.com/posts/"
  :on-preview="handlePreview"
  :on-remove="handleRemove"
  :file-list="fileList">
  <i class="el-icon-upload"></i>
  <div class="el-upload-dragger__text">将文件拖到此处,或<em>点击上传</em></div>
  <div class="el-upload__tip" slot="tip">只能上传jpg/png文件且不超过500kb</div>
</el-upload>

:::

Attribute

参数 说明 类型 可选值 默认值
action 必选参数, 上传的地址 string
headers 可选参数, 设置上传的请求头部 object
multiple 可选参数, 是否支持多选文件 boolean
data 可选参数, 上传时附带的额外参数 object
name 可选参数, 上传的文件字段名 string file
with-credentials 支持发送 cookie 凭证信息 boolean false
show-upload-list 是否显示已上传文件列表 boolean true
type 上传控件类型 string select,drag select
accept 可选参数, 接受上传的文件类型thumbnail-mode 模式下此参数无效) string
on-preview 可选参数, 点击已上传的文件链接时的钩子, 可以通过 file.response 拿到服务端返回数据 function(file)
on-remove 可选参数, 文件列表移除文件时的钩子 function(file, fileList)
on-success 可选参数, 文件上传成功时的钩子 function(response, file, fileList)
on-error 可选参数, 文件上传失败时的钩子 function(err, response, file)
on-progress 可选参数, 文件上传时的钩子 function(event, file, fileList)
before-upload 可选参数, 上传文件之前的钩子,参数为上传的文件,若返回 false 或者 Promise 则停止上传。 function(file)
thumbnail-mode 是否设置为图片模式,该模式下会显示图片缩略图 boolean false
fileList 默认已上传的文件列表, 例如: [{name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}] array []

Methods

方法名 说明 参数
clearFiles 清空已上传的文件列表