• Element UI上传图片和PDF,支持预览


     背景


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


     做法

      index.vue

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

  • 相关阅读:
    为什么建议你做自动化邮件营销?
    大数据分析&数据仓库关于数据库选型方面的感触
    安全可靠的文件传输服务助力完成更高效的医疗保健工作(下)
    你真的懂synchronized锁?
    Windows与网络基础-3-虚拟机安装Kali Linux
    关于 Tomcat 启动时,解决控制台输出日志乱码问题的方案
    基于Python、wxpython的高校教务系统设计与实现
    js--滚轮事件
    Python 运算符和表达式
    基于ASRPRO智能离线语音识别模块实现人机交流对话应用
  • 原文地址:https://blog.csdn.net/qq_24147051/article/details/139452648