element ui 上传图片以及pdf组件

本文介绍了一个基于Vue的文件上传组件,支持图片及PDF文件的上传、预览、删除等功能,并详细展示了组件配置项及使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

FileUpload.vue

<template>
  <div>
    <el-upload
      :action="action"
      :file-list="fileList"
      list-type="picture-card"
      :limit="limit"
      :accept="accept"
      :class="hideUpload || uploading ? 'hideUpload' : ''"
      :on-error="handleError"
      :before-upload="beforeUpload"
      :on-success="handleImageSuccess"
      multiple
    >

      <i slot="default" class="el-icon-plus"></i>

      <div slot="file" slot-scope="{ file }">
        <div>

        <img
        class="el-upload-list__item-thumbnail"
        :src="isPDF(file.url) ? pdf : file.url"
        alt="" />

        <span
          v-if="file.status === 'success'"
          class="el-upload-list__item-actions"
        >
          <span
            v-if="preview"
            class="el-upload-list__item-preview"
            @click="handlePreview(file)"
          >
            <i class="el-icon-zoom-in"></i>
          </span>
          <span
            v-if="download"
            class="el-upload-list__item-delete"
            @click="handleDownload(file)"
          >
            <i class="el-icon-download"></i>
          </span>
          <span
            v-if="deleted"
            class="el-upload-list__item-delete"
            @click="handleRemove(file)"
          >
            <i class="el-icon-delete"></i>
          </span>
        </span>
        <span
          v-else
          :class="[
            uploading ? 'uploading' : '',
            'el-upload-list__item-actions',
          ]"
        >
          <i class="el-icon-loading" /><i style="font-size: 14px">上传中</i>
        </span>


      </div>

      </div>
    </el-upload>

    <el-dialog :visible.sync="previewVisible">
      <img width="100%" :src="previewImgUrl" alt="" />
    </el-dialog>
  </div>
</template>

<script>

import logo from '@/assets/pdf.png'

export default {
  name: 'FileUpload',
  props: {
    value: {
      type: [String, Array],
      default: ''
    },
    // 上传的地址
    action: {
      type: String,
      default: '', //上传接口
    },
    // 设置上传的请求头部
    headers: {
      type: Object,
      default: () => {
        return {}
      },
    },
    // 上传文件大小限制, 默认 50M,单位M
    size: {
      type: [Number, String],
      default: 50,
    },
    // 文件上传格式, 默认jpeg, png,jpg
    accept: {
      type: String,
      default: 'image/jpeg,image/png',
    },
    // 是否显示删除操作按钮
    deleted: {
      type: Boolean,
      default: true,
    },
    // 是否显示预览操作按钮
    preview: {
      type: Boolean,
      default: true,
    },
    // 是否显示下载操作按钮
    download: {
      type: Boolean,
      default: true,
    },
    // 上传文件个数限制,默认0 不限制
    limit: {
      type: [Number, String],
      default: 0,
    },
  },
  data() {
    return {
      fileList: [], // 默认文件列表
      hideUpload: false, // 超出限制掩藏上传按钮
      uploading: false, // 是否上传中,上传时隐藏上传按钮
      previewImgUrl: '', // 预览图片地址
      previewVisible: false, // 是否显示预览
      pdf: logo,
      files: [], // 文件url数组
    }
  },
  watch:{
    value: {
        handler(val, old){
          console.log('FileUpload=', val)
          if(val){
            this.files = val
            this.fileList = [] // 先清空
            for(var i=0; i<val.length;i++){
              this.fileList.push({
                url: val[i] // 转化
              })
            }
            this.handleChange()
          }
      },
      deep: true,
      immediate: true // 避免在子组件中监听失效
    }
  },
  methods: {
    emitInput() {
      this.$emit('input', this.files)
    },
    // 判断是否pdf
    isPDF(url) {
      if(!url){
        return false
      }
      const fileType = ['pdf']
      const index = url.lastIndexOf('.')
      const type = url.substr(index + 1)
      return fileType.indexOf(type) > -1
    },
    // 文件上传成功
    handleImageSuccess(res) {
      if (res.code === 200) {
        this.files.push(res.data.file)
        this.emitInput()
      } else {
        this.$message.error('文件上传失败')
      }
    },
    // 上传前文件大小判断
    beforeUpload(file) {
      const { size } = this
      const overSize = size > 0 && file.size < 1024 * 1024 * size
      if (!overSize) this.$message.error(`上传文件大小不能超过 ${size}MB!`)
      this.uploading = overSize // 是否上传中
      return overSize
    },
    // 上传出错返回
    handleError(event, file, fileList) {
      console.log(event, file, fileList, 'error')
      this.$message.error('服务出错,上传失败!')
      this.handleChange()
    },
    // 删除图片
    async handleRemove(file) {
      this.$confirm(`确认删除文件?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { fileList } = this
        this.files = this.files.filter((v) => v !== file.url)
        this.emitInput()
      }).catch(()=>{})
    },
    // 图片预览
    handlePreview(file) {
      if(this.isPDF(file.url)){
        window.open(file.url, "_blank");
      } else {
        this.previewImgUrl = file.url
        this.previewVisible = true
      }
    },
    handleChange(file, list) {
      const { limit, fileList } = this
      if (limit > 0 && fileList.length >= limit) this.hideUpload = true
      else this.hideUpload = false
      this.uploading = false
    },
    handleDownload(file) {
      window.open(file.url, "_blank");
      /*const a = document.createElement('a')
      a.href = file.url
      a.click() // 模拟点击事件,实现图片文件的同源下载
      */
    },
  },
}
</script>

<style lang="scss" scoped>
.hideUpload .el-upload--picture-card {
  display: none;
}
.el-upload-list--picture-card .uploading.el-upload-list__item-actions {
  opacity: 1;
}
/*添加、删除文件时去掉动画过渡*/
.el-upload-list__item {
  transition: none !important;
}

.el-upload-list--picture-card .el-upload-list__item-thumbnail {
    width: 148px;
    height: 148px;
}
.el-upload-list--picture-card .el-upload-list__item {
    background-color: inherit;
    border: none;
    width: 148px;
    height: 148px;
}
</style>
 

在main.js中引入并全局注册:

import FileUpload from '@/components/upload/FileUpload.vue'

Vue.component('FileUpload', FileUpload) // 图片/pdf 添加和编辑

使用:

 <div>
        <FileUpload
        v-model=""  //绑定数据
        accept=".jpg, .jpeg, .png, .pdf"
        />
      </div>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值