SoFunction
Updated on 2025-04-04

vue3 certificate identification upload component packaging function

ID picture recognition uploadAccording to business needs, it is often involved in document uploads, such as ID card uploads, bank card, business license and other information. According to the designer's design, an upload component is separately encapsulated. The interface backend is used by Alibaba Cloud.
The upload component uses the code on element-plus el-upload:

<template>
  <div class="component-upload-image">
    <el-upload 
      :action="uploadUrl" 
      list-type="picture-card" 
      :on-success="handleUploadSuccess"
      :before-upload="handleBeforeUpload" 
      :limit="limit" 
      :on-error="handleUploadError" 
      :on-exceed="handleExceed"
      ref="imageUpload" 
      :before-remove="handleDelete" 
      :data="params" 
      name="file" 
      :show-file-list="true" 
      :headers="headers"
      v-model:file-list="fileList" 
      :on-preview="handlePictureCardPreview" 
      :class="{ hide:  >= limit }">
        <el-icon class="avatar-uploader-icon">
          <plus />
        </el-icon>
      </el-upload>
    <!-- Upload prompts -->
    <div class="el-upload__tip" v-if="showTip">
      Please upload
      <template v-if="fileSize">
        No more than the size <b style="color: #f56c6c">{{ fileSize }}MB</b>
      </template>
      <template v-if="fileType">
        The format is <b style="color: #f56c6c">{{ ("、") }}</b>
      </template>
      Files
    </div>
    <template v-if="showTipButton">
      <el-button link type="primary" @click="showDiagram = !showDiagram" icon="InfoFilled">View schematic diagram</el-button>
    </template>
    <el-image class="tip-img" :src="diagram" fit="contain" :preview-src-list="[diagram]" v-if="showDiagram" :preview-teleported="true"></el-image>
    <el-dialog v-model="dialogVisible" title="Preview" width="800px" append-to-body>
      <img :src="dialogImageUrl" style="display: block; max-width: 100%; margin: 0 auto" />
    </el-dialog>
  </div>
</template>
<script setup name="CertificateUpload">
import { getToken } from "@/utils/auth";
const props = defineProps({
  // Whether to display a schematic diagram  showTipButton: {
    type: Boolean,
    default: true
  },
  // Schematic diagram  diagram: { 
    type: String,
    default: '',
  },
  //Identify the interface  action:{
    type: String,
    default: '',
  },
  modelValue: [String, Object, Array],
  limit: {
    type: Number,
    default: 1,
  },
  // Size limit (MB)  fileSize: {
    type: Number,
    default: 5,
  },
  // File type, for example ['png', 'jpg', 'jpeg']  fileType: {
    type: Array,
    default: () => ['png','jpg','jpeg','bmp'],
  },
  // Whether to display a prompt  isShowTip: {
    type: Boolean,
    default: true
  },
});
const { proxy } = getCurrentInstance();
const emit = defineEmits(['update:modelValue','certificateInfo']);
const showDiagram = ref(false)
const dialogImageUrl = ref("");
const dialogVisible = ref(false);
const baseUrl = .VITE_APP_BASE_API;
// const uploadUrl = ref(baseUrl + ); // uploaded image server addressconst headers = ref({ Authorization: "Bearer " + getToken() , repeatSubmit : false});
const uploadUrl = computed( //Uploaded image server address  () => baseUrl + 
);
const showTip = computed(
  () =>  && ( || )
);
const params = ref({})
const fileList = computed({
  get: () => {
    if(){
      // First convert the value into an array      if(!()){
        proxy.$('modelValue Must be an array { url ossId name }[]')
        return []
      }
      return 
    }else{
      return []
    }
  },
  set: (newVal) => {}
});
// Loading before uploadingfunction handleBeforeUpload(file) {
  try {
    let isImg = false;
    if () {
      let fileExtension = "";
      if ((".") > -1) {
        fileExtension = ((".") + 1);
      }
      isImg = (type => {
        if ((type) > -1) return true;
        if (fileExtension && (type) > -1) return true;
        return false;
      });
    } else {
      isImg = ("image") > -1;
    }
    if (!isImg) {
      proxy.$(
        `File format is incorrect, Please upload${("/")}Image format file!`
      );
      return false;
    }
    if () {
      const isLt =  / 1024 / 1024 < ;
      if (!isLt) {
        proxy.$(`The uploaded image size cannot exceed ${} MB!`);
        return false;
      }
    }
     = (/\.[a-zA-Z]+$/g, '') + '_' + new Date().getTime() // Put a timestamp to avoid repeated overwriting of file addresses    proxy.$("Uploading pictures, please wait...");
  } catch (error) {
    proxy.$();
    (error)
  }
}
// The number of files exceedsfunction handleExceed() {
  proxy.$(`The number of uploaded files cannot exceed ${} indivual!`);
}
// Callback successfully uploadedfunction handleUploadSuccess(res, file, fileList) {
  try {
    if ( === 200) {
      // Multiple pictures      // const all = [, ...(z=>!).map(u=>)].join(',')
      emit("update:modelValue", [{
        url: ,
        name: ,
        ossId: 
      }])
      emit("certificateInfo",)
      proxy.$();
    } else {
      proxy.$();
      proxy.$();
      proxy.$(file);
    }
  } catch (error) {
    proxy.$();
    (error)
  }
}
// Delete the picturefunction handleDelete(file) {
  const findex = (f => ).indexOf();
  if (findex > -1) {
    (findex, 1);
    emit("update:modelValue", (z => ).join(','))
    return false;
  }
}
// Upload failedfunction handleUploadError() {
  proxy.$("Uploading image failed");
  proxy.$();
}
// Previewfunction handlePictureCardPreview(file) {
   = ;
   = true;
}
</script>
<style scoped lang="scss">
:deep(.hide .el-upload--picture-card) {
  display: none;
}
</style>

This is the article about the packaging of the vue3 certificate identification upload component. For more related contents of vue3 certificate identification upload, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!