el-upload中的事件

 

 

<template>
<div class="home">
<!-- <h2>Upload 上传</h2> -->
<!--
before-upload: 上传文件之前的钩子,参数为上传的文件,若返回 false 或者返回 Promise 且被 reject,则停止上传。
on-success: 文件上传成功时的钩子 function(response, file, fileList)
on-change: 文件状态改变时的钩子,添加文件、上传成功和上传失败时都会被调用 function(file, fileList)
on-exceed:定义超出限制时的行为 function(files, fileList), limit的时候有用
limit:限制上传文件的个数 最大允许上传个数 number
multiple: 是否支持多选文件 boolean
on-preview:点击文件列表中已上传的文件时的钩子
before-remove:阻止文件移除操作 删除文件之前的钩子,参数为上传的文件和文件列表,若返回 false 或者返回 Promise 且被 reject,则停止删除。 function(file, fileList)
on-remove: 文件列表移除文件时的钩子 function(file, fileList)
file-list: 上传的文件列表, 例如: [{name: 'food.jpg', url: 'https://xxx.cdn.com/xxx.jpg'}]
-->
<el-upload
class="upload-demo"
ref="fileUpload"
:action="url"
:before-upload="beforeUpload"
:on-success="uploadSuccess"
:on-change="fileListChange"
:on-exceed="handleExceed"
multiple
:on-preview="handlePreview"
:before-remove="beforeRemove"
:on-remove="handleRemove"
:file-list="fileList"
accept=".xls, .xlsx"
>
<el-button type="primary" :loading="提交中">这里点击上传</el-button>
<div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div>
</el-upload>
<br><br><br>
<p>{{fileName}}</p>
<p>{{fileUrl}}</p>
</div>
</template>

<script>
import url from '@/assets/js/interfaceURL.js'
export default {
name: 'home',
data () {
return {
url: url.Common.baseUrl + url.Common.getImgUrl,
fileList: [],
fileName: '',
fileUrl: '',
提交中: false
}
},
methods: {
beforeUpload (file) {
this.提交中 = true
let isLimit = file.size / 1024 / 1024 < 50
if (!isLimit) {
this.$message.error('文件最大限制50M!')
}
return isLimit
},
uploadSuccess (response, file, fileList) {
if (response.code && response.code === '1') {
this.提交中 = false
fileList.splice(0, fileList.length - 1)
this.fileList[0] = file
this.fileUrl = response.data.imageUrl[0]
this.fileName = file.name
} else {
this.提交中 = false
this.fileList = []
this.fileUrl = ''
this.fileName = ''
this.$message.error('上传文件格式有误,请使用模板重新提交!')
}
},
fileListChange (file, fileList) {
},
handleExceed (files, fileList) {
this.$message.warning(`当前限制选择 3 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + fileList.length} 个文件`)
},
handlePreview (file) {
console.log('handlePreview', file)
},
beforeRemove (file, fileList) {
return this.$confirm(`确定移除 ${file.name}?`)
},
handleRemove (file, fileList) {
}
}
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>

------------------------------

<el-upload
class="avatar-uploader"
:action="url"
: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>
</el-upload>

data () {
return {
url: url.Common.baseUrl + url.Common.getImgUrl,
imageUrl: ''
}
},
methods: {
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
},
handleAvatarSuccess (res, file) {
this.imageUrl = URL.createObjectURL(file.raw)
}
}

----------------------

 

 

<template>
  <div class="多级账户管理" style="height:100%">
    <!--
      show-file-list 是否显示已上传文件列表 boolean
    -->
    <el-upload
      class="avatar-uploader"
      :action="url"
      list-type="picture-card"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload"
      :on-preview="handlePictureCardPreview"
      :on-remove="handleRemove"
    >
      <i class="el-icon-plus"></i>
      <div class="el-upload__text">添加图片</div>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible" title="图片预览" custom-class="preview-dialog">
      <el-carousel>
        <el-carousel-item v-for="(item,index) in previewUrlList" :key="index">
          <img width="100%" :src="item.url" alt />
        </el-carousel-item>
      </el-carousel>
    </el-dialog>
    <br />
    <br />
    <br />
  </div>
</template>

<script>
import url from '@/assets/js/interfaceURL.js'
export default {
  name: '多级账户管理',
  data () {
    return {
      url: url.Common.baseUrl + url.Common.getImgUrl,
      imageUrlList: [],
      previewUrlList: [],
      dialogVisible: false
    }
  },
  methods: {
    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
    },
    handleAvatarSuccess (res, file) {
      this.imageUrlList.push({
        name: file.name,
        size: file.size,
        url: res.data.imageUrl[0]
      })
    },
    handlePictureCardPreview (file) {
      let currentUrlObj = this.imageUrlList.find(item => item.url === file.response.data.imageUrl[0])
      this.previewUrlList = this.imageUrlList.filter(item => {
        return item.url !== file.response.data.imageUrl[0]
      })
      this.previewUrlList.unshift(currentUrlObj)
      this.dialogVisible = true
    },
    handleRemove (file, fileList) {
      this.imageUrlList = this.imageUrlList.filter(item => {
        return item.url !== file.response.data.imageUrl[0]
      })
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<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: #409eff;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.el-upload--picture-card {
  display: inline-flex;
  display: -webkit-inline-box;
  display: -ms-inline-flexbox;
  flex-direction: column;
  justify-content: center;
  line-height: 1;
}
.el-upload__text {
  line-height: 16px;
  margin-top: 10px;
  font-size: 12px;
  color: #879399;
}
</style>
posted @ 2020-06-18 15:03  耿鑫  阅读(4960)  评论(0编辑  收藏  举报