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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM