MrShi
2 天以前 39fc2d6754953e41a7334a2166347baacfcfb40a
admin/src/components/common/UploadFile.vue
@@ -2,16 +2,21 @@
  <div>
    <el-upload
      class="upload-demo"
      :accept="uploadData.fileTyp"
      :accept="uploadData.fileType"
      :action="uploadImgUrl"
      :limit="1"
      :limit="this.fileLength"
      :on-exceed="handleExceed"
      :on-success="handleFileSuccess"
      :on-error="uploadError"
      :before-upload="beforeFileUpload"
      :file-list="fileList">
    <el-button size="small" type="primary">点击上传</el-button>
    <div slot="tip" class="el-upload__tip">只能上传{{ uploadData.fileType }}文件,且不超过5mb</div>
    <div slot="tip" class="el-upload__tip"  >
      只能上传{{ this.fileLength }}个文件,
      {{uploadData.fileType?('只支持上传【'+uploadData.fileType+'】类型文件'):''}}
      文件大小不能超过30MB,
      数量超过请删除已传附件重新上传
     </div>
  </el-upload>
  </div>
@@ -29,35 +34,52 @@
      type: String,
    default: 'width: 190px; height: 190px;'
    },
    fileLength:{
      type: Number,
      default: () => 1
    },
    uploadData: Object
  },
  data() {
    return {
      fileList:null,
      uploadImgUrl: process.env.VUE_APP_API_PREFIX + '/public/upload?folder='+this.uploadData.folder
      fileList: [],
      uploadImgUrl: process.env.VUE_APP_API_PREFIX + '/visitsAdmin/cloudService/public/upload?folder='+this.uploadData.folder
    }
  },
  methods: {
    // 上传图片
    handleExceed(){},
    handleExceed(){
      if(this.fileList && this.fileList.length >= this.fileLength){
        this.$message.error('只允许上传【'+this.fileLength+'个附件】')
      }
    },
    handleFileSuccess(res, file) {
      if (res.code == 200) {
        let { data } = res
        // this.fileList = [{name: data.originname, url: data.url }]
        this.fileList = [{name: data.originname, url: data.url }]
        this.$message.success('上传成功')
        this.$emit('uploadSuccess', { fileurl: data.imgaddr, fileurlFull: data.url, name: data.originname })
        this.$emit('uploadSuccess', { fileurl: data.imgaddr, fileurlFull: data.url, name: data.originname,fileSize:data.fileSize })
      } else {
        this.$tip.apiFailed('上传失败')
        this.$message.error('上传失败')
      }
      this.$emit('uploadEnd')
    },
    uploadError() {
      this.$tip.apiFailed('上传失败')
      this.$emit('endUpload')
      this.$message.error('上传失败')
      this.$emit('uploadEnd')
    },
    // // 拦截
    //
    beforeFileUpload(file) {
      if(this.fileList && this.fileList.length >= this.fileLength){
        this.$message.error('只允许上传【'+this.fileLength+'个文件】')
        return false;
      }
      const isLt2M = file.size / 1024 / 1024 <= 30; // 检查文件大小是否小于30MB
      if (!isLt2M) {
        this.$message.error('上传文件大小不能超过 30MB!');
        return false; // 不允许上传超过指定大小的文件
      }
      this.$emit('uploadBegin')
      return true
    }
@@ -67,8 +89,9 @@
<style lang="scss" scoped>
$image-width: 100px;
$image-height: 30px;
.avatar-uploader {
  width: $image-width;
  width: 100px;
  height: $image-width;
}
::v-deep .el-upload {
@@ -77,7 +100,7 @@
  cursor: pointer;
  position: relative;
  width: $image-width;
  height: $image-width;
  height: $image-height;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
@@ -88,12 +111,12 @@
  font-size: 28px;
  color: #8c939d;
  width: $image-width;
  height: $image-width;
  height: $image-height;
  text-align: center;
}
.avatar {
  width: $image-width;
  height: $image-width;
  height: $image-height;
  display: block;
}
.tips-style {
@@ -109,15 +132,14 @@
  width: 90px !important;
  height: 90px !important;
}
::v-deep .el-upload-list__item {
  width: 80% !important;
  height: 50px !important;
}
.icon {
  -webkit-transform: translate(-50%,-50%);
  -ms-transform: translate(-50%,-50%);
  transform: translate(0%, -85%);
}
::v-deep .el-upload-list__item {
  width: 500px !important;
  height: 30px !important;
}
</style>