当前位置:   article > 正文

vue+element ui 文件上传之文件缩略图缩略图_vue3+ts使用element的上传怎么上次文件显示图片

vue3+ts使用element的上传怎么上次文件显示图片

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

  1. <el-upload
  2. :action="uploadFileServiceUrl"
  3. list-type="picture-card"
  4. :on-success="(response, file, fileList) => {handleSuccess(response, file, fileList)}"
  5. :file-list="fileList"
  6. accept="image/jpg,image/jpeg,image/png"
  7. :before-upload="handleBeforeUpload"
  8. ref="upload"
  9. :auto-upload="true"
  10. >
  11. <i slot="default" class="el-icon-plus"></i>
  12. <div slot="file" slot-scope="{file}">
  13. <img class="el-upload-list__item-thumbnail" :src="file.url" alt />
  14. <span class="el-upload-list__item-actions">
  15. <span
  16. class="el-upload-list__item-preview"
  17. @click="handlePictureCardPreview(file)"
  18. >
  19. <i class="el-icon-zoom-in"></i>
  20. </span>
  21. <!-- <span
  22. v-if="!disabled"
  23. class="el-upload-list__item-delete"
  24. @click="handleDownload(file)"
  25. >
  26. <i class="el-icon-download"></i>
  27. </span> -->
  28. <span
  29. v-if="!disabled"
  30. class="el-upload-list__item-delete"
  31. @click="handleRemove(file)"
  32. >
  33. <i class="el-icon-delete"></i>
  34. </span>
  35. </span>
  36. </div>
  37. </el-upload>
  38. <!-- 图片预览 -->
  39. <el-dialog :visible.sync="dialogVisible">
  40. <img width="100%" :src="dialogImageUrl" alt />
  41. </el-dialog>

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

 完整代码:

  1. <el-upload
  2. :action="uploadFileServiceUrl"
  3. list-type="picture-card"
  4. :on-success="(response, file, fileList) => {handleSuccess(response, file, fileList)}"
  5. :file-list="fileList"
  6. accept="image/jpg,image/jpeg,image/png"
  7. :before-upload="handleBeforeUpload"
  8. ref="upload"
  9. :auto-upload="true"
  10. >
  11. <i slot="default" class="el-icon-plus"></i>
  12. <div slot="file" slot-scope="{file}">
  13. <img class="el-upload-list__item-thumbnail" :src="file.url" alt />
  14. <span class="el-upload-list__item-actions">
  15. <span
  16. class="el-upload-list__item-preview"
  17. @click="handlePictureCardPreview(file)"
  18. >
  19. <i class="el-icon-zoom-in"></i>
  20. </span>
  21. <!-- <span
  22. v-if="!disabled"
  23. class="el-upload-list__item-delete"
  24. @click="handleDownload(file)"
  25. >
  26. <i class="el-icon-download"></i>
  27. </span> -->
  28. <span
  29. v-if="!disabled"
  30. class="el-upload-list__item-delete"
  31. @click="handleRemove(file)"
  32. >
  33. <i class="el-icon-delete"></i>
  34. </span>
  35. </span>
  36. </div>
  37. </el-upload>
  38. <!-- 图片预览 -->
  39. <el-dialog :visible.sync="dialogVisible">
  40. <img width="100%" :src="dialogImageUrl" alt />
  41. </el-dialog>
  42. export default {
  43. name: "",
  44. data() {
  45. submitForm4: {
  46. fileIdList: []
  47. },
  48. uploadFileServiceUrl: 'xxx', // 文件上传的接口地址
  49. },
  50. methods: {
  51. handleSuccess(response, file, fileList){
  52. response.data.forEach((item,index) => {
  53. this.submitForm4.fileIdList.push(item.fileID)
  54. })
  55. },
  56. // 判断上传的是否为图片
  57. handleBeforeUpload(file) {
  58. var img = file.name.substring(file.name.lastIndexOf('.') + 1)
  59. const suffix = img === 'jpg'
  60. const suffix2 = img === 'png'
  61. const suffix3 = img === 'jpeg'
  62. // const isLt1M = file.size / 1024 / 1024 < 1;
  63. if (!suffix && !suffix2 && !suffix3) {
  64. this.$message.error("只能上传图片!");
  65. return false
  66. }
  67. // 可以限制图片的大小
  68. // if (!isLt1M) {
  69. // this.$message.error('上传图片大小不能超过 1MB!');
  70. // }
  71. return suffix || suffix2 || suffix3
  72. },
  73. handleRemove(file,fileList) {
  74. const uploadFiles = this.$refs.upload.uploadFiles;
  75. for (let i in uploadFiles) {
  76. if (file.url === uploadFiles[i].url) {
  77. uploadFiles.splice(i, 1);
  78. }
  79. }
  80. // console.log(uploadFiles,'uploadFilesuploadFiles')
  81. this.getFileID(uploadFiles)
  82. },
  83. getFileID(fileList){
  84. let deleFileId = []
  85. fileList.forEach((item,index) => {
  86. console.log(index,item.response.data)
  87. item.response.data.forEach((item,index) => {
  88. // console.log(index,item)
  89. deleFileId.push(item.fileID)
  90. })
  91. })
  92. this.submitForm4.fileIdList = deleFileId
  93. },
  94. handlePictureCardPreview(file) {
  95. this.dialogImageUrl = file.url;
  96. this.dialogVisible = true;
  97. },
  98. }
  99. }

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/275681
推荐阅读
相关标签
  

闽ICP备14008679号