a-upload自定义上传图片

这是一个Vue组件示例,用于实现图片上传功能。组件使用了Ant Design Vue库,支持图片格式和大小验证,并在上传前检查图片尺寸是否符合1920x268的要求。如果图片不符合尺寸,将提示错误;否则,会调用`uploadArticleFile`接口上传文件,并在成功后显示上传成功的消息,同时触发父组件的`uploadImg`事件。
摘要由CSDN通过智能技术生成
<template>
  <div>
    <a-upload
      class="upload-btn"
      :action="'#'"
      :show-upload-list="false"
      :customRequest="httpRequest"
      :before-upload="beforeUpload"
    >
      顶部图片
    </a-upload>
  </div>
</template>

<script>
import { uploadArticleFile } from '@/api/edit'
export default {
  components: {},
  data () {
    return {}
  },
  mounted () {
  },
  methods: {
   async httpRequest (file) {
      const formData = new FormData()
      const _this = this
      const reader = new FileReader()
      reader.readAsDataURL(file.file)
      reader.addEventListener('loadend', function (e) {
       const image = new Image()
       image.src = e.target.result
       image.onload = async function () {
         if (image.width !== 1920 || image.height !== 268) {
           _this.$message.error('需要符合要求尺寸的图片!')
           return false
         } else {
           formData.append('file', file.file)
           const { code, data } = await uploadArticleFile(formData)
           if (code === 200) {
             _this.$message.success('上传成功!')
             _this.$emit('uploadImg', data.url)
           } else {
             _this.$message.error('上传失败!请重新上传')
           }
         }
       }
     })
    },
    // 检查图片格式和图片大小, 限制1920尺寸
    async beforeUpload (file) {
      // 图片格式限制为 jpeg、png
      const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png'
      if (!isJpgOrPng) {
        this.$message.error('请选择jpg/png图片')
        return false
      }
      // 图片大小限制为 10M以内
      const isLt10M = file.size / 1024 / 1024 <= 10
      if (!isLt10M) {
        this.$message.error('图片大小不能超过10MB,请重新上传')
        return false
      }
      return isJpgOrPng && isLt10M
    }
  }
}
</script>
<style lang="less" scoped>
.upload-btn {
  width: 100%;
  height: 76px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: #fafafa;
  border: 1px dashed #d9d9d9;
  border-radius: 2px;
  cursor: pointer;
  margin-bottom: 10px;

  /deep/ .ant-upload {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>

好的,我会尽力回答你关于 arco design vue 二次封装 a-upload 的问题。a-upload 是 arco design vue 中的一个上传组件,可以用于上传文件和图片。在二次封装 a-upload 时,你可以根据自己的需求对其进行定制。 一般来说,二次封装 a-upload 的步骤如下: 1. 引入 a-upload 组件 在你的组件中,首先需要引入 a-upload 组件。可以使用以下代码: ```javascript import { AUpload } from 'arco-design-vue' ``` 2. 编写自定义上传组件 然后,你需要编写自定义上传组件,并在其中使用 a-upload 组件。在上传前可以添加自己的逻辑,例如判断文件类型、大小等,并通过 a-upload 的 before-upload 属性实现。代码如下: ```html <template> <div> <a-upload :before-upload="beforeUpload" :on-success="onSuccess" :on-error="onError" :on-progress="onProgress" > <a-button type="primary">上传文件</a-button> </a-upload> </div> </template> <script> import { AUpload, AButton } from 'arco-design-vue' export default { name: 'CustomUpload', components: { AUpload, AButton }, methods: { beforeUpload(file) { // 自定义逻辑,例如判断文件类型、大小等 console.log('beforeUpload', file) return true }, onSuccess(response, file) { console.log('onSuccess', response, file) }, onError(error, response, file) { console.log('onError', error, response, file) }, onProgress(event, file, fileList) { console.log('onProgress', event, file, fileList) } } } </script> ``` 3. 使用自定义上传组件 最后,在需要使用上传组件的地方,你可以直接使用自定义上传组件。例如: ```html <template> <div> <custom-upload></custom-upload> </div> </template> <script> import CustomUpload from './CustomUpload.vue' export default { name: 'App', components: { CustomUpload } } </script> ``` 这样,你就完成了 arco design vue 二次封装 a-upload 组件的过程。希望我的回答能够对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值