自定义el-upload 回显图标, 或展示图片

需求 上传图片展示图片,上传文档展示文档图标
在这里插入图片描述

		<el-upload class="upload-demo Dialog"
                       :file-list="multipleFileList"
                       drag
                       multiple
                       name="file"
                       accept='pdf,docx,txt,xlsx,ppt,mp4,MP4,avi,png,gif,jpg,jpeg'
                       :before-remove='removeSingle'
                       :on-success='successfun'
                       :before-upload="beforeUpload"
                       :http-request='uploadImg'>
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">点击上传</div>
            </el-upload>
            <ul class="imgListDelete">
		        <li v-for="item in imgarry"
		            :key="item">
		          <el-image :src="item"
		                    :preview-src-list="imgarry">
		          </el-image>
		        </li>
      		</ul>
data(){
return{
	multipleFileList: [],
	imgarry: [],
	}
}
methods:{
successfun (response, file, fileList) {
      localStorage.removeItem('img')
    },
    async uploadImg (f) {
      let param = new FormData()
      param.append('file', f.file)
      const { data: res } = await axios.post('http://enn-product.test.fnwintranet.com/enn/product/file/oss/upload', param,
        {
          headers: {
            'Content-Type': 'multipart/form-data'
          }
        })
      if (!res.success) return this.$message.error('文件上传失败')
      this.$message.success('文件上传成功')
      let houzhui = f.file.name.split('.') // 获取上传文件的后缀
      let title = document.getElementsByClassName('el-icon-document')[this.multipleFileList.length]
      // [fileList.length-1]  这个需要特别注意,需要查看当前页面不上传之前有多少个后才能根据具体情况具体分析
      if (houzhui[1] == 'png' || houzhui[1] == 'gif' || houzhui[1] == 'jpg' || houzhui[1] == 'jpeg') {
        title.parentNode.style.display = 'none'
        let filetemp = f.file
        this.getBase64(filetemp).then((res) => {
          console.log(filetemp);
          let url = res
          this.imgarry.push(url)
        })
        title.className = 'imgiconPDF'
      } else if (houzhui[1] == 'pdf') {
        title.className = 'imgiconPDF'
      } else if (houzhui[1] == 'docx') {
        title.className = 'imgiconPDF imgicondocx'
      } else if (houzhui[1] == 'txt') {
        title.className = 'imgiconPDF imgicontxt'
      } else if (houzhui[1] == 'ppt') {
        title.className = 'imgiconPDF imgiconppt'
      } else if (houzhui[1] == 'xlsx') {
        title.className = 'imgiconPDF imgiconxel'
      } else if (
        houzhui[1] == 'MP4' ||
        houzhui[1] == 'mp4' ||
        houzhui[1] == 'avi'
      ) {
        title.className = 'imgiconvideo'
      } else {
        title.classList.add('el-icon-document') // 其他默认文档
      }
      this.file.attrValue = res.data.id
      this.file.file = res.data
      localStorage.setItem('img', JSON.stringify(this.file))
      this.fileList.push(JSON.parse(localStorage.getItem('img')))
    },
    getBase64 (file) {
      console.log(file);
      return new Promise((resolve, reject) => {
        let reader = new FileReader()
        let fileResult = ''
        reader.readAsDataURL(file)
        //开始转
        reader.onload = function () {
          fileResult = reader.result
        }
        //转 失败
        reader.onerror = function (error) {
          reject(error)
        }
        //转 结束  咱就 resolve 出去
        reader.onloadend = function () {
          resolve(fileResult)
        }
      })
    },
    // 文件删除
    removeSingle (file, fileList) {
      let ext = file.name.split('.')
      let arr = this.fileList.filter(item => {
        if (item.file.size !== file.raw.size) {
          return item
        }
      })
      if (ext[1] == 'png' || ext[1] == 'gif' || ext[1] == 'jpg' || ext[1] == 'jpeg') {
        this.getBase64(file.raw).then((res) => {
          this.imgarry.splice(this.imgarry.findIndex(item => item === res), 1)
        })
      }
      this.fileList = arr
    },

    beforeUpload (file) {
      const fileSuffix = file.name.substring(file.name.lastIndexOf('.') + 1)
      const whiteList = ['pdf', 'docx', 'txt', 'xlsx', 'ppt', 'mp4', 'MP4', 'avi', 'png', 'gif', 'jpg', 'jpeg']

      if (whiteList.indexOf(fileSuffix) === -1) {
        this.$message.error('上传文件只能是 pdf,docx,txt,xlsx,ppt,mp4,MP4,avi,png,gif,jpg,jpeg格式')
        return false
      }
      const isLt1M = file.size / 1024 / 1024 < 100
      if (!isLt1M) {
        this.$message.error('上传文件大小不能超过 100MB')
        return false
      }
    },
}
.Dialog /deep/ .el-icon-document {
  display: none;
}
.Dialog /deep/ .el-upload-list__item .el-icon-upload-success {
  color: #67c23a;
  margin-top: 13px;
}
.Dialog /deep/ .el-upload-list__item .el-icon-close {
  top: 10px;
}
.Dialog /deep/ .imgiconvideo {
  display: inline-block;
  width: 50px;
  height: 50px;
  background-size: 100% 100%;
  margin-right: 10px;
  background-image: url("../../assets/images/视频@2x.png");
}
.Dialog /deep/ .imgiconPDF {
  display: inline-block;
  width: 50px;
  height: 50px;
  background-size: 100% 100%;
  margin-right: 10px;
  background-image: url("../../assets/images/PDF@2x.png") !important;
}
.Dialog /deep/ .imgicondocx {
  background-image: url("../../assets/images/WOR@2x.png") !important;
}
.Dialog /deep/ .imgicontxt {
  background-image: url("../../assets/images/TXT@2x.png") !important;
}
.Dialog /deep/ .imgiconppt {
  background-image: url("../../assets/images/PPT@2x.png") !important;
}
.Dialog /deep/ .imgiconxel {
  background-image: url("../../assets/images/XEL@2x.png") !important;
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值