文件选择器 FilePicker

基本用法

  1. <div class="uploader-wrapper" style="display:flex;padding:15px;">
  2. <za-badge sup v-for="(i, index) in files" class="uploader-item" shape="circle" :key="index" @click="remove(index)" style="display:inline-block;margin-right:15px;align-items: center;justify-content: center;width:74px;height:74px;border:2px solid #ddd;">
  3. <za-icon type="wrong" slot="text" style="font-size:10px;"></za-icon>
  4. <div class="uploader-item-img">
  5. <a :href="i.thumbnail" target="_blank"><img :src="i.thumbnail" alt></a>
  6. </div>
  7. </za-badge>
  8. <div class="uploader-wrapper">
  9. <za-file-picker class="uploader-btn" style="display:flex;align-items: center;justify-content: center;width:74px;height:74px;border:2px dashed #ddd;" accept="image/jpg, image/jpeg, image/gif, image/png" @change="handleChange">
  10. <za-icon type="add" style="fontSize:30px;">
  11. </za-icon></za-file-picker>
  12. </div>
  13. </div>

多选模式

  1. <div class="uploader-wrapper" style="display:flex;padding:15px;">
  2. <za-badge sup v-for="(i, index) in fileList" class="uploader-item" shape="circle" :key="index" @click="remove2(index)" style="display:inline-block;margin-right:15px;align-items: center;justify-content: center;width:74px;height:74px;border:2px solid #ddd;">
  3. <za-icon type="wrong" slot="text" style="font-size:10px;"></za-icon>
  4. <div class="uploader-item-img">
  5. <a :href="i.thumbnail" target="_blank"><img :src="i.thumbnail" alt></a>
  6. </div>
  7. </za-badge>
  8. <div class="uploader-wrapper">
  9. <za-file-picker v-if="fileList.length < 5" multiple class="uploader-btn" style="display:flex;align-items: center;justify-content: center;width:74px;height:74px;border:2px dashed #ddd;" :before-select="beforeSelect" accept="image/jpg, image/jpeg, image/gif, image/png" @change="handleChangeMulti">
  10. <za-icon type="add">
  11. </za-icon></za-file-picker>
  12. </div>
  13. </div>
  14. <za-toast :visible.sync="visible" :duration="1000">删除成功</za-toast>

禁用状态

<div class="uploader-wrapper" style="display:flex;padding:15px;">
  <div class="uploader-wrapper">
    <za-file-picker class="uploader-btn" style="display:flex;align-items: center;justify-content: center;width:74px;height:74px;border:2px dashed #ddd;" disabled>
      <za-icon type="add" style="fontSize:30px;">
    </za-icon></za-file-picker>
  </div>
</div>

Vue Script

<script name="vue">
export default {
  data() {
    return {
      files: [],
      fileList: [],
      visible: false,
    }
  },
  methods: {
    handleChange(data){
      console.log(data);
      this.files.push(data)
    },
    handleChangeMulti(dataList){
      if(dataList.length + this.fileList.length > 5){
        alert('超过5张')
        this.fileList = this.fileList.concat(...(dataList.slice(0, 5 - this.fileList.length)));
      }else{
        this.fileList = this.fileList.concat(...dataList);
      }
    },
    remove(index){
      this.files.splice(index, 1);
      this.visible = true
    },
    remove2(index){
      this.fileList.splice(index, 1);
      this.visible = true
    },
    beforeSelect(){
      if(this.fileList.length > 5){
        alert('超过5张')
        return false
      }else{
        alert('before select')
      }
    }
  },
};
</script>

API

Uploader

属性类型默认值可选值/参数说明
acceptstring允许上传的附件格式,参考 File 文件类型
multiplebooleanfalse是否支持多选
disabledbooleanfalse是否禁用

Uploader Event

事件名称说明回调参数
before-select选择前触发的事件() => boolean
change值变化时触发的回调函数。multiple 为 true 时,返回文件数组格式,否则为文件对象(file?: object | object[]) => void

FilePicker 文件选择器 - 图1