使用el-upload上传图片,改造预览功能

今天给大家介绍一下在使用Elementel-upload上传图片的过程中,改造原有的预览功能。

官网预览效果图:

在这里插入图片描述

改造之后的预览效果:

在这里插入图片描述

区别如下

  • 官网的预览效果,只能单纯预览当前图片
  • 改造之后,可以对预览图片进行放大、缩小、旋转、下一张等操作

实现代码

<el-upload
    action="#"
    multiple
    list-type="picture-card"
    :class="{'is-not-allow-access':picIDListCount>=3}"
    :file-list="imageUrls"
    :on-remove="handleRemove"
    :on-preview="handlePictureCardPreview"
    :on-success="uploadSuccess"
  >
    <span> <i class="el-icon-plus" style="font-size:14px;" ></i> 添加图片</span>
  </el-upload>
  <!-- 大图预览 -->
  <el-image-viewer
      v-if="showImgViewer"
      :on-close="closeImgViewer"
      :url-list="imagePreviewUrls"
      :z-index="3000"
      :initial-index="initialImgPreviewIndex"
  />
import ElImageViewer from 'element-ui/packages/image/src/image-viewer'
  components: {
      ElImageViewer
  },
  data(){
    return {
      initialImgPreviewIndex:0, // 预览打开看到的图片下标
      imagePreviewUrls:[], // 预览图片的下标
    }
  },
  methods:{
    // 图片预览
    handlePictureCardPreview (file) {
      let index = this.imagePreviewUrls.indexOf(file.url)
      if (index >= 0) {
        this.initialImgPreviewIndex = index
      }
      this.showImgViewer = true
    },
    closeImgViewer () {
      this.showImgViewer = false
    },
    // 上传成功回调
    uploadSuccess (file, fileList) {
      if (fileList.status === 'success') {
        this.imagePreviewUrls.push(fileList.url) // 预览图片的数组
      }
    },
    // 删除选中的文件
    handleRemove (file) {
      // 删除预览数组对应的图片
      let index = this.imagePreviewUrls.indexOf(file.url)
      if (index > -1) {
        this.imagePreviewUrls.splice(index, 1)
      }
      // 执行删除
    },

  }

新选择了图片,要添加到预览图片数组中【见 uploadSuccess 方法】

在删除图片后,也要同步删除预览数组(imagePreviewUrls)中的该图 【见 handleRemove 方法】

在点击预览的时候,要传入预览组件要打开图片的下标【见 handlePictureCardPreview 方法】

以上代码为elementUI未暴露出来的组件,感兴趣的同学可以看下源码,在node_modules/element-ui/packages/image/src/image-viewer,还有许多可配置的参数

  • 6
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
要实现el-upload上传文件后点击预览功能,可以通过以下步骤实现: 1. 在el-upload组件中添加一个自定义的slot,用于显示预览按钮。 2. 在handleFileChange函数中,将上传的文件保存到一个数组中,同时为每个文件生成一个唯一的id。 3. 在预览按钮的点击事件中,根据文件的id获取对应的文件内容,并将其显示在一个弹窗中。 4. 在弹窗中添加一个删除按钮,用于删除当前预览的文件。 代码示例: ``` <el-upload class="upload-demo" ref="uploadFile" :auto-upload="false" :action="testForm.actionUrl" drag :file-list="testForm.fileList" :on-change="handleFileChange" :on-remove="removeFile" :limit="1" :on-exceed="overLimitCount" > <el-button slot="trigger" size="small" type="primary">选择文件</el-button> <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div> <el-button slot="preview" size="small" type="text" @click="showPreview">预览</el-button> </el-upload> data() { return { testForm: { fileList: [], fileContentList: [] }, previewFileId: '', isShowPreview: false } }, methods: { handleFileChange(files, fileList) { this.testForm.fileList = fileList files.forEach(file => { const reader = new FileReader() reader.readAsText(file.raw) reader.onload = e => { const fileContent = e.target.result.replace(/\n|\r\n/g, '<br/>') this.testForm.fileContentList.push({ id: file.uid, content: fileContent }) } }) }, showPreview() { if (this.testForm.fileContentList.length > 0) { this.previewFileId = this.testForm.fileContentList[0].id this.isShowPreview = true } }, removeFile(file, fileList) { const index = this.testForm.fileContentList.findIndex(item => item.id === file.uid) if (index !== -1) { this.testForm.fileContentList.splice(index, 1) } }, removePreview() { const index = this.testForm.fileContentList.findIndex(item => item.id === this.previewFileId) if (index !== -1) { this.testForm.fileContentList.splice(index, 1) this.isShowPreview = false } } }, computed: { previewFileContent() { const file = this.testForm.fileContentList.find(item => item.id === this.previewFileId) return file ? file.content : '' } } ```
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值