vue+element ui 文件上传之文件缩略图缩略图

文件缩略图按官方文档说的是使用 scoped-slot 去设置缩略图模版。且需要上传的是图片,因为有预览等功能,如果上传的不是图片,会显示不出来。

<el-upload
  :action="uploadFileServiceUrl"
  list-type="picture-card"
  :on-success="(response, file, fileList) => {handleSuccess(response, file, fileList)}"
  :file-list="fileList"
  accept="image/jpg,image/jpeg,image/png" 
  :before-upload="handleBeforeUpload"
  ref="upload"
  :auto-upload="true"
>
  <i slot="default" class="el-icon-plus"></i>
  <div slot="file" slot-scope="{file}">
    <img class="el-upload-list__item-thumbnail" :src="file.url" alt />
    <span class="el-upload-list__item-actions">
      <span
        class="el-upload-list__item-preview"
        @click="handlePictureCardPreview(file)"
      >
        <i class="el-icon-zoom-in"></i>
      </span>
      <!-- <span
        v-if="!disabled"
        class="el-upload-list__item-delete"
        @click="handleDownload(file)"
      >
        <i class="el-icon-download"></i>
      </span> -->
      <span
        v-if="!disabled"
        class="el-upload-list__item-delete"
        @click="handleRemove(file)"
      >
        <i class="el-icon-delete"></i>
      </span>
    </span>
  </div>
</el-upload>
<!-- 图片预览 -->
<el-dialog :visible.sync="dialogVisible">
  <img width="100%" :src="dialogImageUrl" alt />
</el-dialog>

这里设置了图片的格式等,用户在选择的时候,会自动校准图片格式,官方文档中提供了before-upload方法,可以防止用户在选择文件的时候使用查看所有文件的方式。

 完整代码:

<el-upload
  :action="uploadFileServiceUrl"
  list-type="picture-card"
  :on-success="(response, file, fileList) => {handleSuccess(response, file, fileList)}"
  :file-list="fileList"
  accept="image/jpg,image/jpeg,image/png" 
  :before-upload="handleBeforeUpload"
  ref="upload"
  :auto-upload="true"
>
  <i slot="default" class="el-icon-plus"></i>
  <div slot="file" slot-scope="{file}">
    <img class="el-upload-list__item-thumbnail" :src="file.url" alt />
    <span class="el-upload-list__item-actions">
      <span
        class="el-upload-list__item-preview"
        @click="handlePictureCardPreview(file)"
      >
        <i class="el-icon-zoom-in"></i>
      </span>
      <!-- <span
        v-if="!disabled"
        class="el-upload-list__item-delete"
        @click="handleDownload(file)"
      >
        <i class="el-icon-download"></i>
      </span> -->
      <span
        v-if="!disabled"
        class="el-upload-list__item-delete"
        @click="handleRemove(file)"
      >
        <i class="el-icon-delete"></i>
      </span>
    </span>
  </div>
</el-upload>
<!-- 图片预览 -->
<el-dialog :visible.sync="dialogVisible">
  <img width="100%" :src="dialogImageUrl" alt />
</el-dialog>
export default {
  name: "",
  data() {
    submitForm4: {
      fileIdList: []
    },
    uploadFileServiceUrl: 'xxx', // 文件上传的接口地址
  },
  methods: {
    handleSuccess(response, file, fileList){
      response.data.forEach((item,index) => {
        this.submitForm4.fileIdList.push(item.fileID)
      })
      
    },
    // 判断上传的是否为图片
    handleBeforeUpload(file) {
      var img = file.name.substring(file.name.lastIndexOf('.') + 1)
      const suffix = img === 'jpg'
      const suffix2 = img === 'png'
      const suffix3 = img === 'jpeg'
      // const isLt1M = file.size / 1024 / 1024 < 1;
      if (!suffix && !suffix2 && !suffix3) {
          this.$message.error("只能上传图片!");
          return false
      }
      // 可以限制图片的大小
      // if (!isLt1M) {
      //     this.$message.error('上传图片大小不能超过 1MB!');
      // }
      return suffix || suffix2 || suffix3
    },
    handleRemove(file,fileList) {
      const uploadFiles = this.$refs.upload.uploadFiles;
      for (let i in uploadFiles) {
        if (file.url === uploadFiles[i].url) {
          uploadFiles.splice(i, 1);
        }
      }
      // console.log(uploadFiles,'uploadFilesuploadFiles')
      this.getFileID(uploadFiles)
    },
    getFileID(fileList){
      let deleFileId = []
      fileList.forEach((item,index) => {
        console.log(index,item.response.data)
        item.response.data.forEach((item,index) => {
          // console.log(index,item)
          deleFileId.push(item.fileID)
          
        })
      })
      this.submitForm4.fileIdList = deleFileId

    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
  }
}

这里删除的时候需要注意的是,缩略图中,删除时没有整体的fileList参数,所以需要使用ref来获取所有上传的图片列表,这样子在删除的时候,就可以确定用户删除的,是哪一张图片了。

最后,附上删除时利用ref获取到的数组格式

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
您好!感谢您的提问。 如果您想要在 Vue 2 中显示图片文件缩略图,您可以使用第三方的图片处理库,比如 [vue-image-size](https://www.npmjs.com/package/vue-image-size) 或 [vue-image-loader](https://www.npmjs.com/package/vue-image-loader)。 下面是一个使用 `vue-image-size` 库来显示图片缩略图的示例代码: ```html <template> <div> <img v-for="file in fileList" :key="file.id" :src="file.url" :width="file.width" :height="file.height" /> </div> </template> <script> import VueImageSize from 'vue-image-size'; export default { components: { VueImageSize }, data() { return { fileList: [ { id: 1, url: 'https://picsum.photos/200/300', width: null, height: null }, { id: 2, url: 'https://picsum.photos/300/200', width: null, height: null }, { id: 3, url: 'https://picsum.photos/400/400', width: null, height: null } ] }; }, mounted() { this.getImageSize(); }, methods: { getImageSize() { this.fileList.forEach((file, index) => { VueImageSize(file.url).then((size) => { this.$set(this.fileList[index], 'width', size.width); this.$set(this.fileList[index], 'height', size.height); }); }); } } }; </script> ``` 在上面的代码中,我们首先使用 `v-for` 指令来遍历图片列表,并使用 `img` 元素来显示每个图片缩略图。其中,`:src` 属性用来指定图片的 URL,`:width` 和 `:height` 属性用来指定图片的宽度和高度。由于图片的宽度和高度需要先获取到才能正确显示缩略图,因此我们需要使用 `vue-image-size` 库来获取图片的大小。具体来说,我们在 `mounted` 钩子函数中调用 `getImageSize` 方法来获取图片的大小,然后使用 `$set` 方法来更新 `fileList` 数组中的每个文件的宽度和高度属性。 希望这个回答能够帮助到您!如果您有任何其他问题,请随时提出。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值