Newer
Older
KaiFengPC / src / views / preassess / evaluation / upload.vue
@zhangdeliang zhangdeliang on 23 May 1 KB 初始化项目
<template>
  <el-upload
    class="upload"
    action="/system/upload"
    :headers="uploadHeader"
    :before-upload="beforeUpload"
    :on-success="onSuccess"
    :on-error="onError"
    :disabled="disabled"
    :file-list="fileList"
  >
    <slot></slot>
  </el-upload>
</template>

<script setup>
import { getToken } from '@/utils/auth';

const props = defineProps({
  disabled: {
    type: Boolean,
    default: false,
  },
  fileList: {
    type: Array,
    default: () => [],
  },
});

const emit = defineEmits(['success']);

const { disabled } = props;

const uploadHeader = ref({
  Authorization: 'Bearer ' + getToken(),
});

const beforeUpload = rawFile => {
  console.log(rawFile);
  if(rawFile.name.length > 50) {
    proxy.$modal.msgError(`文件名称过长(50个字符以内),请先修改再上传!`);
    return false
  }
  return true;
};

const onSuccess = (response, uploadFile, uploadFiles) => {
  const { extension, name, originalName, refField, refType, size, url } = response.data;
  const file = {
    extension,
    name,
    originalName,
    refField,
    refType,
    size,
    url,
  };
  emit('success', file);
};

const onError = (error, uploadFile, uploadFiles) => {
  console.log(error, uploadFile, uploadFiles);
};
</script>

<style lang="scss" scoped>
.upload {
  display: inline-block;
  :deep(.el-upload-list) {
    display: none;
  }
}
</style>