ant 修改upload组件样式,上传样式

效果图
在这里插入图片描述
组件代码 自行参考


<template>
  <div class="img-upload" :class="sizeClass" style="margin-bottom: 10px" :style="'height:'+size+'px;width:'+size+'px'">
    <a-upload
      :action="uploadAction"
      list-type="picture-card"
      :file-list="fileList"
      :beforeUpload="beforeUpload"
      v-bind="$attrs"
      :disabled="disabled"
      :remove="removeFile"
      :accept="acceptType"
      :headers="{workNo: userInfo.workNo, 'X-Access-Token': token}"
    >
      <div v-if="fileList.length < limit">
        <a-icon type="plus" />
        <div class="ant-upload-text">
          {{ placeholder }}
        </div>
        <div style="margin-top:8px;color:rgba(0, 0, 0, 0.4)">
          仅支持 PDFJPGPNG 格式。最大文件尺寸 10 MB
        </div>
        <div style="margin-top:2px;color:rgba(0, 0, 0, 0.4)">
          (图片比例670*320)
        </div>
      </div>
    </a-upload>
    <a-modal :visible="previewVisible" title="预览" :footer="null" @cancel="handleCancel">
      <img v-if="showImagePreview" alt="图片" style="width: 100%" :src="previewImage" />
      <video v-else-if="showVideoPreview&&previewVisible" autoplay style="width: 100%" :src="previewImage"></video>
    </a-modal>
  </div>
</template>

<script>
  import { USER_INFO, ACCESS_TOKEN } from "@/store/mutation-types"
  import Vue from 'vue'
  function getBase64(file) {
    return new Promise((resolve, reject) => {
      const reader = new FileReader();
      reader.readAsDataURL(file);
      reader.onload = () => resolve(reader.result);
      reader.onerror = error => reject(error);
    });
  }
  export default {
    name: "ImgUpload",
    props: {
      uri: {
        type: String,
        default: 'mm/mmWechatWelcomeMsg/upload'
      },
      value: {
        type: Array,
        default: () => ([])
      },
      limit: {
        type: Number,
        default: Infinity
      },
      disabled: {
        type: Boolean,
        default: false
      },
      fileType: {
        type: String,
        default: 'image'
      },
      size: {
        type: Number,
        default: 100
      },
      placeholder: {
        type: String,
        default: '点击上传'
      }
    },
    data () {
      return {
        previewVisible: false,
        previewImage: '',
        fileList: this.value||[],
        userInfo: Vue.ls.get(USER_INFO),
        token: Vue.ls.get(ACCESS_TOKEN)
      }
    },
    watch: {
      value (val) {
        this.fileList = val||[]
      }
    },
    computed: {
      showImagePreview () {
        return ['image', '.jpg', '.png', '.jpeg', '.gif'].some(item => this.fileType.includes(item))
      },
      showVideoPreview () {
        return ['.mp3', '.mp4', '.acc', 'audio', 'video'].some(item => this.fileType.includes(item))
      },
      uploadAction () {
        return `${window._CONFIG['domianURL']}/${this.uri}`;
      },
      sizeClass () {
        return 'img-size-'+this.size
      },
      acceptType () {
        if (this.fileType == 'image') {
          return 'image/*'
        } else if (this.fileType == 'video') {
          return 'video/*'
        } else if (this.fileType == 'audio') {
          return 'audio/*'
        }
        return this.fileType
      }
    },
    methods: {
      beforeUpload(file) {
          return new Promise((resolve, reject) => {
          let isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png'|| file.type === 'image/jpg';
          if (!isJpgOrPng) {
              this.$message.error('格式错误,只能上传jpg、jpeg、png');
              return reject(false);
          }
          let w = 0,h = 0;
          let reader = new FileReader();
              reader.readAsDataURL(file);
              reader.onload=()=>{
                    const image = new Image();
                    image.src = reader.result;
                    image.onload=()=>{
                            w = image.width;
                            h = image.height;
                            const ratio = 670/320
                            if(w/h == ratio){  // 图片比例为670*320横纵比一致
                                return resolve(true);
                            }else{
                              this.$message.error('图片尺寸错误,只能上传670x320横纵比一致的图片');
                              return reject(false);
                            }
                        }
                  }
          let isLt1M = file.size / 10240 / 10240 <= 1;
          if (!isLt1M) {
              this.$message.error('图片大小超过10MB!');
              return reject(false);
          }
          return isJpgOrPng && isLt1M;
          })
      },
      removeFile (file) {
        this.$emit('delete', file)
        if (this.limit == 1) {
          this.$emit('change', [])
        }
        return true
      },
      handleCancel() {
        this.previewVisible = false;
      },
      async handlePreview(file) {
        if (!file.url && !file.preview) {
          file.preview = await getBase64(file.originFileObj);
        }
        this.previewImage = file.url || file.preview;
        this.previewVisible = true;
      },
      handleChange({ file,fileList }) {
        this.fileList = fileList
        if (file.status === 'done') {
          for (let i = this.fileList.length-1;i>=0;--i) {
            const tempFile = this.fileList[i]
            if (tempFile.response && tempFile.response.success) {
              this.$set(this.fileList, i, {
                uid: tempFile.response.result.id,
                name: tempFile.response.result.fileName,
                url: tempFile.response.result.url,
                status: 'done'
              })
            } else if (!tempFile.url){
              this.$message.error('上传失败,请重试!')
              this.fileList.splice(i,1)
            }
          }
          this.$emit('change', this.fileList)
        } else if (file.status === 'error') {
          this.$message.error('上传失败,请重试!')
          this.fileList = this.fileList.filter(item => item.status !== 'error')
        }
        this.$emit('input', this.fileList)
      },
    }
  }
</script>

<style scoped lang="less">
  .img-upload {
    &.img-size-100 {
      /deep/.ant-upload-list-picture-card-container {
        width: 425px;
        height: 152px;
      }
      /deep/.ant-upload.ant-upload-select-picture-card {
        width: 425px;
        height: 152px;
      }
      /deep/.ant-upload-list-picture-card .ant-upload-list-item {
        width: 425px;
        height: 152px;
      }
      .ant-upload-select-picture-card .ant-upload-text {
        color: #666;
        display: block;
      }
    }
    &.img-size-50 {
      /deep/.ant-upload-list-picture-card-container {
        width: 50px;
        height: 50px;
      }
      /deep/.ant-upload.ant-upload-select-picture-card {
        width: 50px;
        height: 50px;
      }
      /deep/.ant-upload-list-picture-card .ant-upload-list-item {
        width: 50px;
        height: 50px;
      }
      .ant-upload-select-picture-card .ant-upload-text {
        margin-top: 8px;
        color: #666;
        display: none;
      }
    }
  }
  /deep/.ant-upload-list-picture .ant-upload-list-item,
  /deep/.ant-upload-list-picture-card .ant-upload-list-item{
    padding: 0;
  }
  .ant-upload-select-picture-card i {
    font-size: 32px;
    color: #999;
  }


</style>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Ant Design的上组件,在Vue 3中进行父组件调用。首先,你需要在父组件引入Ant Design的Upload组件: ``` <template> <div> <a-upload :action="uploadUrl" :headers="headers" :data="data" :multiple="multiple" :before-upload="beforeUpload" :on-progress="onProgress" :on-success="onSuccess" :on-error="onError" :disabled="disabled" > <a-button> <a-icon type="upload"></a-icon> Click to Upload </a-button> </a-upload> </div> </template> <script> import { defineComponent } from 'vue'; import { Upload, Button, Icon } from 'ant-design-vue'; export default defineComponent({ name: 'ParentComponent', components: { 'a-upload': Upload, 'a-button': Button, 'a-icon': Icon }, data() { return { uploadUrl: 'https://www.example.com/upload', headers: {}, data: {}, multiple: true, disabled: false }; }, methods: { beforeUpload() { // Do something before upload }, onProgress() { // Do something on progress }, onSuccess() { // Do something on success }, onError() { // Do something on error } } }); </script> ``` 在父组件中,你需要设置上组件的一些属性,例如上的URL、请求头、数据、是否允许多文件上、是否禁用等等。你还需要定义一些上的回调函数,例如在上之前执行的函数、上进度更新函数、上成功回调函数、上失败回调函数等等。 在模板中,你可以使用`<a-upload>`标签来引用Ant Design的上组件。你还可以使用`<a-button>`标签和`<a-icon>`标签来设置上按钮的样式。 在子组件中,你可以使用`$emit`函数来触发父组件中定义的回调函数。例如,在上成功时,你可以使用以下代码触发`onSuccess`函数: ``` this.$emit('on-success'); ``` 在父组件中,你可以使用`@`符号来绑定子组件触发的事件。例如,你可以使用以下代码来绑定上成功事件: ``` <a-upload ... @on-success="onSuccess" > ``` 这样,当子组件触发上成功事件时,父组件中定义的`onSuccess`函数就会被调用。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值