当前位置:   article > 正文

Element UI上传图片和PDF,支持预览_el-upload上传pdf可预览

el-upload上传pdf可预览

 背景


  如上图,使用Element UI的el-upload组件,并且预览的时候可以展示图片和PDF格式文件;


 做法

  index.vue

  1. <template>
  2. <div>
  3. <el-upload
  4. v-model="diaForm.list"
  5. :limit="5"
  6. :on-exceed="handleExceed"
  7. :on-preview="handlePreview"
  8. :before-upload="beforeAvatarUpload"
  9. :on-remove="handleRemove"
  10. :headers="reqHeaders"
  11. :on-success="onUploadSuccess"
  12. :action="uploadUrl()"
  13. :file-list="resultFileList"
  14. list-type="picture"
  15. class="upload-files"
  16. accept=".png,.jpeg,.gif,.pdf,.jpg,.JPG">
  17. <div class="upfile-btn">
  18. <d2-icon name="document-upload" class="document-upload" />
  19. <div>点击上传+拖拽上传</div>
  20. </div>
  21. </el-upload>
  22. </div>
  23. </template>
  24. <script>
  25. import { workOrderUploadUlr } from '@/api/upload';
  26. export default {
  27. data() {
  28. return {
  29. diaForm: {
  30. desc: '',
  31. list: [],
  32. },
  33. resultFileList: [],
  34. };
  35. },
  36. methods: {
  37. // 限制上传个数不超过5个
  38. handleExceed(files, fileList) {
  39. if (fileList && fileList.length == 5) {
  40. this.$message.warning('XXXX');
  41. }
  42. },
  43. handlePreview(file) {
  44. console.log(file);
  45. },
  46. // 限制不超过30M
  47. beforeAvatarUpload(file) {
  48. const isLt5M = file.size / 1024 / 1024 < 30;
  49. if (!isLt5M) {
  50. this.$message.error('XXXX');
  51. }
  52. return isLt5M;
  53. },
  54. // 移除文件
  55. handleRemove(file, fileList) {
  56. if (!file) {
  57. return;
  58. }
  59. const index = this.resultFileList.findIndex(item => item.uid === file.uid);
  60. this.resultFileList.splice(index, 1);
  61. },
  62. // 设置请求头里面的token 和 userId
  63. reqHeaders() {
  64. return {
  65. token: this.$util.cookies.token,
  66. userId: this.info ? this.info.userId : '',
  67. };
  68. },
  69. // 上传成功,返回
  70. onUploadSuccess(response, file, fileList) {
  71. if (response && response.code === 'APPLY_SUCCESS') {
  72. if (response.data) {
  73. this.resultFileList.push({
  74. url: response.data.url,
  75. name: response.data.name,
  76. uid: file.uid
  77. });
  78. this.dealPDF();
  79. setTimeout(() => {
  80. this.dealPDF();
  81. }, 1);
  82. }
  83. } else if (response && response.msg) { console.log('upload failed', response.msg); }
  84. },
  85. // 上传的服务器的地址
  86. uploadUrl() {
  87. return workOrderUploadUlr();
  88. },
  89. dealPDF() {
  90. var liElements = document.querySelectorAll('ul.el-upload-list.el-upload-list--picture li.el-upload-list__item');
  91. liElements.forEach(function(liElement) {
  92. var aElement = liElement.querySelector('a.el-upload-list__item-name');
  93. if (aElement && aElement.textContent.includes('.pdf')) {
  94. var imgElement = liElement.querySelector('img.el-upload-list__item-thumbnail');
  95. if (imgElement) {
  96. imgElement.src = 'https://sg-pay69e4d75928dac6fddd3229a/file.png'; // 替换为你想要的新图片的URL
  97. }
  98. }
  99. });
  100. },
  101. }
  102. };
  103. </script>
  104. <style>
  105. </style>

upload.js

  1. // import request from '@/plugin/axios';
  2. const host = require('../../host');
  3. // 工单文件上传
  4. export function workOrderUploadUlr(hostType = 'BASIC_GATE') {
  5. return host.getBaseUrl(hostType) + 'xxxxx/file/upload';
  6. }

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号