# Upload 上传
---

### 基础用法

<font color="#666">单个文件上传</font>
<div class="demo-block">
  <y-upload
    class="upload-demo"
    action="https://appdrivert1.yoohoor.com:4000/v1/roads/upload"
    :on-preview="handlePreview"
    :on-remove="handleRemove"
    :before-remove="beforeRemove"
    :on-exceed="handleExceed"
    :limit="1"
    :on-error="handleError"
    :on-success="handleSuccess"
    :file-list="fileList">
    <y-button :type="styleClass" :class="'is-'+styleClass" icon="icon-upload">点击上传</y-button>
    <div slot="tip" class="k-upload__tip">只能上传pdf/jpg/jpeg/bmp/gif/png文件,且不超过5M</div>
  </y-upload>
  <div class="style-toggle">
    <p>切换样式</p>
    <y-radio-group v-model="styleClass" >
      <y-radio label="default">样式一</y-radio>
      <y-radio label="plain">样式二</y-radio>
    </y-radio-group>
  </div>
</div>

<script>
  export default {
    data() {
      return {
        fileList: [],
        imageUrl: '',
        dialogImageUrl: '',
        dialogVisible: false,
        limit: 4,
        styleClass: 'default',
        styleClass2: 'default'
      };
    },
    methods: {
      handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      handlePreview(file) {
        console.log(file);
      },
      handleExceed(files, fileList) {
        this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
      },
      beforeRemove(file, fileList) {
        return this.$confirm(`确定移除 ${ file.name }？`);
      },
      handleAvatarSuccess(res, file) {
        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;
      },
      handleSuccess(res, rawFile) {

      },
      handleError(err, rawFile) {

      }
    }
  }
</script>

::: demo

``` html
<template>
  <y-upload
    class="upload-demo"
    action="https://appdrivert1.yoohoor.com:4000/v1/roads/upload"
    :on-preview="handlePreview"
    :on-remove="handleRemove"
    :before-remove="beforeRemove"
    multiple
    :limit="3"
    :on-exceed="handleExceed"
    :on-error="handleError"
    :on-success="handleSuccess"
    :file-list="fileList">
    <y-button type="default" icon="icon-upload">点击上传</y-button>
    <div slot="tip" class="k-upload__tip">只能上传pdf/jpg/jpeg/bmp/gif/png文件,且不超过5M</div>
  </y-upload>
</template>

```
:::

<font color="#666">多个文件上传</font>

<div class="demo-block">
  <y-upload
    class="upload-demo"
    action="https://appdrivert1.yoohoor.com:4000/v1/roads/upload"
    :on-preview="handlePreview"
    :on-remove="handleRemove"
    :before-remove="beforeRemove"
    multiple
    :limit="limit"
    :on-exceed="handleExceed"
    :on-error="handleError"
    :on-success="handleSuccess"
    :file-list="fileList">
    <y-button :type="styleClass2" :class="'is-'+styleClass2" icon="icon-upload">点击上传</y-button>
    <label class="file-len">[{{fileList.length}}/{{limit}}]</label>
    <div slot="tip" class="k-upload__tip">只能上传pdf/jpg/jpeg/bmp/gif/png文件,且不超过5M</div>
  </y-upload>

  <div class="style-toggle">
    <p>切换样式</p>
    <y-radio-group v-model="styleClass2" >
      <y-radio label="default">样式一</y-radio>
      <y-radio label="plain">样式二</y-radio>
    </y-radio-group>
  </div>
</div>


::: demo

``` html
<template>
  <y-upload
    class="upload-demo"
    action="https://appdrivert1.yoohoor.com:4000/v1/roads/upload"
    :on-preview="handlePreview"
    :on-remove="handleRemove"
    :before-remove="beforeRemove"
    multiple
    :limit="3"
    :on-exceed="handleExceed"
    :on-error="handleError"
    :on-success="handleSuccess"
    :file-list="fileList">
    <y-button type="default" icon="icon-upload">点击上传</y-button>
    <div slot="tip" class="k-upload__tip">只能上传pdf/jpg/jpeg/bmp/gif/png文件,且不超过5M</div>
  </y-upload>
</template>

```
:::

### Attribute
| 参数      | 说明          | 类型      | 可选值                           | 默认值  |
|---------- |-------------- |---------- |--------------------------------  |-------- |
| action | 必选参数，上传的地址 | string | — | — |
| headers | 设置上传的请求头部 | object | — | — |
| multiple | 是否支持多选文件 | boolean | — | — |
| data | 上传时附带的额外参数 | object | — | — |
| name | 上传的文件字段名 | string | — | file |
| with-credentials | 支持发送 cookie 凭证信息 | boolean | — | false |
| show-file-list | 是否显示已上传文件列表 | boolean | — | true |
| accept | 接受上传的[文件类型](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-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 | 显示的文件列表的类型 | string | text | text |
| auto-upload | 是否在选取文件后立即进行上传 | boolean | — | true |
| file-list | 上传的文件列表, 例如: [{name: 'food.jpg', url: 'https://xxx.cdn.com/xxx.jpg'}] | array | — | [] |
| http-request | 覆盖默认的上传行为，可以自定义上传的实现 | function | — | — |
| disabled | 是否禁用 | boolean | — | false |
| limit | 最大允许上传个数 |  number | — | — |
| on-exceed | 文件超出个数限制时的钩子 | function(files, fileList) | — | - |

### Slot
| name | 说明 |
|------|--------|
| trigger | 触发文件选择框的内容 |
| tip | 提示说明文字 |

### Methods
| 方法名      | 说明          | 参数 |
|----------- |-------------- | -- |
| clearFiles | 清空已上传的文件列表（该方法不支持在 before-upload 中调用） | — |
| abort      | 取消上传请求    | （ file: fileList 中的 file 对象 ） |
| submit     | 手动上传文件列表 |  —

<!--
| drag | 是否启用拖拽上传 | boolean | — | false |
| list-type | 显示的文件列表的类型 | string | text/picture(各占一行)/picture-card(卡片排列) | text |
 -->
<!-- <script>
  export default {
    data() {
      return {
      	fileList:[],
        imageUrl: '',
        dialogImageUrl: '',
        dialogVisible: false
      };
    },
    methods: {
      handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      handlePreview(file) {
        console.log(file);
      },
      handleExceed(files, fileList) {
        this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
      },
      beforeRemove(file, fileList) {
        return this.$confirm(`确定移除 ${ file.name }？`);
      },
      handleAvatarSuccess(res, file) {
        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;
      }
    }
  }
</script>
<style scoped>
.index {
  width: 800px;
  margin: 100px auto;
}
.avatar-uploader-icon {
  border: 1px dashed #d9d9d9;
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar-uploader-icon:hover{
  border-color: #409EFF;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

### 基础用法
<div class="demo-block">
	<y-upload
	  action="https://jsonplaceholder.typicode.com/posts/"
	  :on-preview="handlePreview"
	  :on-remove="handleRemove"
	  :before-remove="beforeRemove"
	  multiple
	  :limit="3"
	  :on-exceed="handleExceed"
	  :file-list="fileList">
	  <y-button  type="default">点击上传</y-button>
	  <div class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
	</y-upload>
</div>

### 用户上传头像
<div class="demo-block">
	<y-upload
	  class="avatar-uploader"
	  action="https://jsonplaceholder.typicode.com/posts/"
	  :show-file-list="false"
	  :on-success="handleAvatarSuccess"
	  :before-upload="beforeAvatarUpload">
	  <img v-if="imageUrl" :src="imageUrl" class="avatar">
	  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
	</y-upload>
</div>

### 拖拽上传
<y-upload
  class="upload-demo"
  drag
  action="https://jsonplaceholder.typicode.com/posts/"
  multiple>
  <i class="el-icon-upload"></i>
  <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
  <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
</y-upload> -->






