ant design vue 照片墙实现本地预览 查看大图以及删除 手动上传多图

内容是根据网友的案例进行修改,代码直接可以用,给大家参考参考,有不足之处请大家指点。

代码:

<template>
<!-- 先预览,然后再自定义上传base64图片 -->
  <div class="clearfix">
    <a-upload
      listType="picture-card"
      :fileList="fileList"
      :beforeUpload="beforeUpload"
      :multiple="true"
      :remove="handleRemove"
      @preview="handlePreview"
    >
      <div v-if="fileList.length < 9">
        <a-icon type="plus" />
        <div class="ant-upload-text">Upload</div>
      </div>
    </a-upload>
    <!-- 放大预览窗口 -->
    <a-modal :visible="previewVisible" :footer="null" @cancel="handleCancel">
        <img alt="example" style="width: 100%" :src="previewImage" />
      </a-modal>
    <a-button type="primary"
    :loading="uploading"
    @click="handleUpload"
    style="margin-top: 16px">点击上传</a-button>
      
  </div>
</template>
<script>
import reqwest from 'reqwest';
export default {
  data () {
    return {
      fileList: [],
      previewVisible: false, 
      previewImage: "",
      uploading: false,
    }
  },
  methods: {
    beforeUpload (file) {
      if (this.fileList.length > 9) {//判断上传的图片数量
         
        return false
      }
      this.exeBase(file)
      return false
    },
    async exeBase (file) {
      await this.getBase64(file, (imageUrl) => {
        if (this.fileList.length < 9 && this.preventRepeat(imageUrl)) {//判断图片数量及去重
          const file2 = file
          file2.url = imageUrl
          this.fileList = [...this.fileList, file]
          this.loading = false
        }
      }).then((result) => {
        console.log(result)
      })
    },
    handleRemove (file) {//删除图片
      const index = this.fileList.indexOf(file)
      const newFileList = this.fileList.slice()
      newFileList.splice(index, 1)
      this.fileList = newFileList
    },
    getBase64 (file, callback) {
      return new Promise((resolve) => {
        let reader = new FileReader()
        reader.readAsDataURL(file)
        reader.onload = () => {
          callback(reader.result)
          resolve('成功')
        }
      })
    },
    preventRepeat (url) {
      // 排除相同图片
      for (let i = 0, len = this.fileList.length; i < len; i++) {
        if (this.fileList[i].url === url) {
          return false
        }
      }
      return true
    },
    handleCancel() {//点击关闭图片预览
      this.previewVisible = false;
    },
    async handlePreview(file) {//点击查看大图
      if (!file.url && !file.preview) {
        file.preview = await  this.getBase64(file.originFileObj);
      }
      this.previewImage = file.url || file.preview;
      this.previewVisible = true;
    },
    handleUpload() {
      const { fileList } = this;
      const formData = new FormData();
      fileList.forEach(file => {
        formData.append('files[]', file);//后面再加上token
      });
      this.uploading = true;
      reqwest({
        url: 'https://www.mocky.io/v2/5cc8019d300000980a055e76',
        method: 'post',
        processData: false,
        data: formData,
        success: () => {
        //   this.fileList = [];图片上传后清空
          this.uploading = false;
          this.$message.success('upload successfully.');
        },
        error: () => {
          this.uploading = false;
          this.$message.error('upload failed.');
        },
      });
    },
  }
}
</script>

参考资源链接:https://blog.csdn.net/weixin_39651562/article/details/100010828

  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
可以通过使用Ant Design VueUpload组件和Dragger组件实现图片上传并拖拽位置。具体代码示例如下: ``` <template> <div> <a-upload :action="uploadUrl" :before-upload="beforeUpload" :on-success="onSuccess" > <a-icon type="upload" /> Click to Upload </a-upload> <a-dragger :action="uploadUrl" :before-upload="beforeUpload" :on-success="onSuccess" > <p class="ant-upload-drag-icon"> <a-icon type="inbox" /> </p> <p class="ant-upload-text">Click or drag file to this area to upload</p> <p class="ant-upload-hint">Support for a single or bulk upload.</p> </a-dragger> <img :src="imageUrl" width="200" height="200" style="margin-top: 20px;"> </div> </template> <script> export default { data() { return { imageUrl: '', uploadUrl: 'your-upload-server-url' }; }, methods: { beforeUpload(file) { // 在这里可以对文件进行校验,例如大小、类型等 console.log('before upload', file); }, onSuccess(response, file, fileList) { // 上传成功后的回调函数 console.log('upload success', response, file, fileList); this.imageUrl = URL.createObjectURL(file.raw); } } }; </script> ``` 在这段代码中,我们使用了`a-upload`组件和`a-dragger`组件来展示图片上传的界面。`a-upload`组件可以通过单击上传按钮或者拖拽到上传区域来完成文件上传;`a-dragger`组件则是将上传区域改为了一个拖拽框,视频等其他多格式的文件也能方便的拖拽上传。同时,我们在`beforeUpload`函数中可以对文件进行校验,并在`onSuccess`函数中处理上传成功后的事件。最后设置`imageUrl`的值为上传图片的预览链接即可。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值