antdv实现图片的上传、预览以及删除功能

需求:

1.实现图片的上传和预览功能
2.图片不能超过规定的张数
3.预览时,图片可以进行上一张和下一张的切换

html部分代码

<a-button type="primary" ghost
          size="small" class="margin-right10"
          @click="pictureSend(record, index)">上传图片</a-button>
<a-button type="primary"
          size="small"
          @click="picturePreview(record, index)">图片预览</a-button>
<a-modal title="上传图片" v-model="visibleA" ok-text="保存" @ok="handleSaveA" @cancel="handleCancelA">
  <div class="clearfix">
    <a-upload
      action="https://www.mocky.io/v2/5cc8019d300000980a055e76"
      list-type="picture"
      :file-list="fileList"
      :multiple="true"
      :remove="handleFileRemove"
      @preview="handlePreview"
      @change="handleChangePicture"
    >
      <a-button block v-if="fileList.length < 8"> <a-icon type="upload"/> 点击上传 </a-button>
    </a-upload>
  </div>
</a-modal>
<a-modal title="图片预览" v-model="visibleB" ok-text="关闭" @ok="handleCancelB" @cancel="handleCancelB">
  <!-----将需要预览的图片放置在model框中--->
  <viewer :images="fileList">
    <img v-for="item in fileList" :src="item.url" :key="item.uid" style="padding:0 10px 10px 0;width: calc(100% /3);height:160px;"/>
  </viewer>
</a-modal>

js部分代码如下:

<script>
// 引入v-viewer库
import 'viewerjs/dist/viewer.css'
import Viewer from 'v-viewer'
import Vue from 'vue'
Vue.use(Viewer)
export default {
  data() {
    return {
      // 图片路径
      fileList: [
        {
          uid: '-1',
          name: 'image1.png',
          status: 'done',
          url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png'
        },
        {
          uid: '-2',
          name: 'image2.png',
          status: 'done',
          url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png'
        },
        {
          uid: '-3',
          name: 'image3.png',
          status: 'done',
          url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png'
        },
        {
          uid: '-4',
          name: 'image4.png',
          status: 'done',
          url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png'
        }
      ],
      visibleA: false,
      visibleB: false
    }
  },
  methods: {
    // 上传图片按钮
    pictureSend() {
      this.visibleA = true
    },
    // 图片预览
    picturePreview() {
      this.visibleB = true
    },
    // 保存
    handleSaveA() {},
    // 取消
    handleCancelA() {
      this.visibleA = false
    },
    // 关闭
    handleCancelB() {
      this.visibleB = false
    },
    // 预览图片调用的函数---》使点击时不进入预览详情
    handlePreview() {
      return false
    },
    // 文件删除
    handleFileRemove (file) {
      console.log(file)
      this.$message.warn('您点击了删除')
    },
    // 上传图片
    handleChangePicture({ fileList }) {
      if (fileList.length <= 8) {
        this.fileList = fileList
      } else {
        this.$message.error('图片最多不能超过八张!')
      }
    }
  }
}
</script>

注意:
:multiple=“true”—>支持多选文件
上传图片功能的最终效果如图:
在这里插入图片描述
预览图片功能的最终效果如图:在这里插入图片描述

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
首先,我们需要在前端使用 antdv 组件实现图片上传。可以使用 Upload 组件来实现。具体代码如下: ``` <template> <div> <a-upload :action="'http://localhost:8080/api/upload'" :headers="{Authorization: 'Bearer ' + token}" :before-upload="beforeUpload" :on-success="onUploadSuccess" :show-upload-list="false" > <a-button icon="upload">Click to Upload</a-button> </a-upload> </div> </template> <script> export default { data() { return { token: 'your_token_here' } }, methods: { beforeUpload(file) { const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png'; if (!isJpgOrPng) { this.$message.error('You can only upload JPG/PNG file!'); return false; } const isLt2M = file.size / 1024 / 1024 < 2; if (!isLt2M) { this.$message.error('Image must smaller than 2MB!'); return false; } return true; }, onUploadSuccess(response) { this.$message.success('Upload successfully!'); } } } </script> ``` 在这个代码中,我们通过设置 Upload 组件的属性实现了以下功能: - 设置上传的地址为 `http://localhost:8080/api/upload`,这是后端提供的图片上传接口; - 设置请求头中的 `Authorization` 属性为我们的 token,这是为了验证用户的身份; - 设置上传前的验证函数 `beforeUpload`,用于检查上传的文件类型和大小是否符合要求; - 设置上传成功后的回调函数 `onUploadSuccess`,用于在页面上提示用户上传成功。 接下来,我们需要在后端使用 SpringBoot 来接收前端上传图片。具体代码如下: ```java @RestController @RequestMapping("/api") public class UploadController { @PostMapping("/upload") public ResponseEntity<?> uploadFile(@RequestParam("file") MultipartFile file) { try { // 保存上传的文件到本地磁盘或云存储 // ... return ResponseEntity.ok().body("Upload successfully!"); } catch (Exception e) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to upload file."); } } } ``` 在这个代码中,我们通过 `@PostMapping("/upload")` 注解来指定接收前端上传图片请求。`@RequestParam("file") MultipartFile file` 表示接收一个名为 `file` 的文件参数。在接收到图片后,我们可以将其保存到本地磁盘或云存储中,并返回一个上传成功的信息给前端。如果上传过程中出现了异常,我们会返回一个 500 错误给前端。 以上就是使用 antdv 组件和 SpringBoot 来实现图片上传的基本流程。当然,具体的实现方式还会因为不同的业务需求而有所差异。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值