ant-design上传图片


@PostMapping("/file/new")
    public List<Map> getFileUrl(@RequestParam("file[]") MultipartFile[] file) {
        // System.out.println(file);
        log.info("file {}",file.length);
        if(file==null || file.length==0) {
            return Collections.emptyList();
        }
        System.out.println(file.length+"->"+file);

        return Stream.of(file)
                .map(f->fileService.uploadOneFile(f))
                .map(Result::getData)
                .map(url -> {
                    return ViewObject.of("url",url,"name",url,"error",0).getModel();
                })
                .collect(Collectors.toList());
    }

<template>
  <div class="clearfix">
    <a-upload
      :action="uploadURL"
      list-type="picture-card"
      :file-list="fileList"
      @preview="handlePreview"
      @change="handleChange"
    >
      <div v-if="fileList.length < this.uploadNum">
        <a-icon type="plus"/>
        <div class="ant-upload-text">
          上传
        </div>
      </div>
    </a-upload>
    <a-modal :visible="previewVisible" :footer="null" @cancel="handleCancel">
      <img alt="example" style="width: 100%" :src="previewImage"/>
    </a-modal>
  </div>
</template>
<script>
  import {uploadURL} from "../../config/baseconfig";

  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 {
    props: {
      uploadNum: {
        type: Number,
        default: 1
      }
    },
    data() {
      return {
        uploadURL: uploadURL,
        previewVisible: false,
        previewImage: '',
        fileList: [
          {
            uid: '-1',
            name: 'image.png',
            status: 'done',
            url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
          },

        ],
      };
    },
    methods: {
      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(info) {
        console.log(info)
        const formData = new FormData()
        formData.append('file[]', info.file.originFileObj)
        this.$api.post(this.uploadURL, formData,{
          headers:{
            'Content-Type':'multipart/form-data'
          }
        })
          .then(res => {
            console.log(res)
          }).catch(err => {
          console.log(err)
        })
        this.fileList = info.fileList;
      },
    },

    mounted() {
      console.log(this.uploadURL)
    }
  };
</script>
<style>
  /* you can make up upload button and sample style by using stylesheets */
  .ant-upload-select-picture-card i {
    font-size: 32px;
    color: #999;
  }

  .ant-upload-select-picture-card .ant-upload-text {
    margin-top: 8px;
    color: #666;
  }
</style>


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值