Ant Design上传组件upload上传文件404问题

问题描述:

当使用upload组件的时候会出现404的情况,生产环境会出现 405的情况,是因为upload上传组件有默认行为,只需要阻止默认上传行为即可

解决:

方法一:

阻止上传组件的默认行为:上传拦截的方法中返回false即可

代码:

      <a-upload
        v-if="
          needUpload &&
          (!curFileList?.length ||
            curFileList?.length < (fileConfig?.maxLength ? fileConfig?.maxLength : 9))
        "
        :multiple="multiple"
        list-type="picture-card"
        accept="image/*"
        :show-upload-list="false"
        :before-upload="beforeUpload"
      >
        <div>
          <plus-outlined></plus-outlined>
          <div class="hf-fu-text">上传图片</div>
        </div>
      </a-upload>
  // 上传拦截
  const beforeUpload = async (file) => {
    console.log(file)
    const isPNG =
      file.type === 'image/png' ||
      file.type === 'image/jpg' ||
      file.type === 'image/jpeg' ||
      file.type === 'image/gif'
    if (!isPNG) {
      return message.warn('请按规定格式上传文件!')
    }

    // let fileList = e.target.files
    // for (let i = 0; i < fileList.length; i++) {
    //   const element = fileList[i]
    //   emit('iptChange', element, 100)
    // }
    if (props?.imgSize) {
      const size = await handleImgBase(file)
      if (size.imgWidth !== props.imgSize.width || size.imgHeight !== props.imgSize.height) {
        message.warn('请按规定图片尺寸上传!')
        return false
      }
    }
    const size = props?.fileConfig?.size ? props?.fileConfig?.size : 20
    emit('iptChange', file, size)

    //阻止上传组件的默认行为
    return false
  }

解决:

方法二:

代码:

      <a-upload
        :multiple="multiple"
        list-type="picture-card"
        accept="image/*"
        :show-upload-list="false"
        :before-upload="beforeUpload"
        :customRequest="customRequest"
      >
        <div>
          <plus-outlined></plus-outlined>
          <div class="hf-fu-text">上传图片</div>
        </div>
      </a-upload>

  const customRequest= (data) => {

  }

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Ant Design上传组件,在Vue 3中进行父组件调用。首先,你需要在父组件引入Ant DesignUpload组件: ``` <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、付费专栏及课程。

余额充值