antd vue form upload 上传

<template>
  <div>
    <a-form :form="form" :label-col="{ span: 5 }" :wrapper-col="{ span: 18 }">
    
      <a-form-item label="图片" :label-col="{ span: 5 }" :wrapper-col="{ span: 18 }">
        <a-upload
            name="file"
            action="https://www.mocky.io/v2/5cc8019d300000980a055e76"
            :file-list="fileList"
            :customRequest="customRequest"
            list-type="picture-card"
            @preview="handlePreview"
            :remove="handelRemove"
             v-decorator="[
            'img',
            {
              rules: [
                { required: true, message: '请上传图片' }
              ],
              initialValue: fileList, //关键语句
              valuePropName: 'img', //关键语句
              getValueFromEvent: normFiles //关键语句
            }
          ]"
          >
          <div v-if="fileList.length < 1">
            <a-icon type="plus" />
            <div class="ant-upload-text">
              上传
            </div>
          </div>
          </a-upload>
      </a-form-item>
    </a-form>
    <a-modal :visible="previewVisible" :footer="null" @cancel="previewVisible=false">
      <img alt="example" style="width: 100%" :src="previewImage" />
    </a-modal>
  <div>
</template>

<script>
import { addCommodity } from '@/api/reservationRecycling'
export default {
  data() {
    return {
      form: this.$form.createForm(this),
      fileList:[],
      previewVisible:false,
      previewImage:''
    }
  },
  created() {
    this.getOfficialList()
  },
  methods: {
    save() {
      this.form.validateFields((err, fieldsValue) => {
        if (err) {
          return
        }
       
        if(this.fileList.length>0){
          fieldsValue.img=this.fileList[0].url
        }
        addCommodity(fieldsValue).then(({ code, msg }) => {
          if (code != 0) return this.$message.error(msg)
        })
      })
    },
// 我这里是阿里云的方式上传 可以根据自己不同的上传方式作出修改,但fileList的格式要保持一样
customRequest(data) {
      this.fileList=[data.file]
      var client = new OSS({
          region: '---',
          accessKeyId: '---',
          accessKeySecret: '---',
          bucket: '---'
        })
        let address = 'exchangeCommodity/'+new Date().getTime() + data.file.name
        client
          .multipartUpload(address, data.file)
          .then(response => {
            this.fileList=[{url:response.res.requestUrls[0].split('?')[0],status:'done',uid:'-1',name:response.name}]
          })
    },
    handelRemove(){
      this.fileList=[]
    },
    async handlePreview(file) {
      this.previewImage = file.url 
      this.previewVisible = true;
    },
    normFiles(e) {
      return e && e.fileList
    }
  }
}
</script>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值