在Element Ui中支持从体系粘贴版中获取图片和PDF,Docx,Doc,PPT等文档

[复制链接]
发表于 2025-12-27 18:48:43 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
在上一篇中,我们单纯的实现了Ctrl + V实现从粘贴版中获取图片信息,但是点击上传的时间会有个bug,就是点击文件上传的时间,会出现一个bug,这篇,我们将在上一篇的根本上举行完满,并支持从粘贴版中获取文件内容,同时对上传文件的巨细举行判断,且支持PDF,Docx,Doc,PPT等文件举行上传。
实现步调


  • 安装 Element UI
    确保你已经安装了 Element UI 并在项目中精确设置。
  • 创建拖拽上传组件
    假设你已经有一个根本的拖拽上传组件,我们将在其根本上添加 Ctrl + V 功能。
  • 监听粘贴事故
    我们须要在页面中监听 paste 事故,当用户按下 Ctrl + V 时,捕捉粘贴板中的文件数据。
  • 处理处罚粘贴事故
    在捕捉到文件数据后,将其转换为 File 对象,并调用上传方法。
实当代码
  1. <template>
  2.   <div>
  3.     <el-upload
  4.       drag
  5.       action="https://jsonplaceholder.typicode.com/posts/"
  6.       :on-preview="handlePreview"
  7.       :on-remove="handleRemove"
  8.       :before-upload="beforeUpload"
  9.       :on-success="handleSuccess"
  10.       multiple
  11.       ref="upload"
  12.       :file-list="fileList"
  13.     >
  14.       <i class="el-icon-upload"></i>
  15.       <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
  16.       <div class="el-upload__tip" slot="tip">支持上传多种文件类型</div>
  17.     </el-upload>
  18.     <!-- 显示上传后的文件 -->
  19.     <div v-for="(file, index) in fileList" :key="index" class="uploaded-file">
  20.       <div v-if="isImage(file.name)">
  21.         <img :src="file.url" alt="Uploaded Image" class="uploaded-image" />
  22.         <el-button type="text" @click="removeFile(index)">移除</el-button>
  23.       </div>
  24.       <div v-else>
  25.         <span>{{ file.name }}</span>
  26.         <el-button type="text" @click="removeFile(index)">移除</el-button>
  27.       </div>
  28.     </div>
  29.   </div>
  30. </template>
复制代码
CSS
  1. <style scoped>
  2. .uploaded-file {
  3.   margin-top: 10px;
  4.   display: flex;
  5.   align-items: center;
  6. }
  7. .uploaded-image {
  8.   max-width: 100px;
  9.   max-height: 100px;
  10.   margin-right: 10px;
  11. }
  12. </style>
复制代码
JavaScript
  1. <script>
  2. import { Upload } from 'element-ui';
  3. export default {
  4.   name: 'DragUpload',
  5.   data() {
  6.     return {
  7.       fileList: []
  8.     };
  9.   },
  10.   methods: {
  11.     handlePaste(event) {
  12.       const items = event.clipboardData.items;
  13.       for (let i = 0; i < items.length; i++) {
  14.         const allowedTypes = [
  15.           'image/jpeg', 'image/png',
  16.           'application/pdf',
  17.           'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', // DOC, DOCX
  18.           'application/vnd.ms-powerpoint', 'application/vnd.openxmlformats-officedocument.presentationml.presentation' // PPT, PPTX
  19.         ];
  20.         if (allowedTypes.includes(items[i].type)) {
  21.           const file = items[i].getAsFile();
  22.           this.handleFile(file);
  23.           break;
  24.         }
  25.       }
  26.     },
  27.     handleFile(file) {
  28.       const reader = new FileReader();
  29.       reader.onload = (e) => {
  30.         this.fileList.push({
  31.           name: file.name,
  32.           url: e.target.result
  33.         });
  34.       };
  35.       reader.readAsDataURL(file);
  36.       this.$refs.upload.handleStart(file);
  37.       this.$refs.upload.submit();
  38.     },
  39.     handlePreview(file) {
  40.       console.log('Preview:', file);
  41.     },
  42.     handleRemove(file, fileList) {
  43.       this.fileList = fileList;
  44.     },
  45.     beforeUpload(file) {
  46.       const allowedTypes = [
  47.         'image/jpeg', 'image/png',
  48.         'application/pdf',
  49.         'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', // DOC, DOCX
  50.         'application/vnd.ms-powerpoint', 'application/vnd.openxmlformats-officedocument.presentationml.presentation' // PPT, PPTX
  51.       ];
  52.       const isAllowedType = allowedTypes.includes(file.type);
  53.       const isLt500K = file.size / 1024 < 500;
  54.       if (!isAllowedType) {
  55.         this.$message.error('只能上传 JPG/PNG/PDF/DOC/DOCX/PPT/PPTX 格式的文件!');
  56.       }
  57.       if (!isLt500K) {
  58.         this.$message.error('文件大小不能超过 500KB!');
  59.       }
  60.       return isAllowedType && isLt500K;
  61.     },
  62.     handleSuccess(response, file, fileList) {
  63.       // 更新 fileList
  64.       this.fileList = fileList.map(f => ({
  65.         name: f.name,
  66.         url: f.url || f.response.url // 假设服务器返回的响应中有 url 字段
  67.       }));
  68.     },
  69.     removeFile(index) {
  70.       this.fileList.splice(index, 1);
  71.     },
  72.     isImage(fileName) {
  73.       return fileName.toLowerCase().endsWith('.jpg') || fileName.toLowerCase().endsWith('.png');
  74.     }
  75.   },
  76.   mounted() {
  77.     document.addEventListener('paste', this.handlePaste);
  78.   },
  79.   beforeDestroy() {
  80.     document.removeEventListener('paste', this.handlePaste);
  81.   }
  82. };
  83. </script>
复制代码
阐明


  • HTML部分:


  • 使用 el-upload 组件创建一个拖拽上传地域。
  • 使用 v-for 循环遍历 fileList 数组,表现每个上传的文件。
  • 假如文件是图片,表现图片预览和移除按钮。
  • 假如文件不是图片,表现文件名和移除按钮。

  • CSS部分:


  • 添加了一些根本样式来美化上传后的文件表现。

  • JavaScript部分:


  • fileList 数据属性用于存储上传的文件列表。
  • handlePaste 方法:捕捉粘贴事故,查抄粘贴板中的数据是否为支持的文件范例(图片、PDF、DOC、DOCX、PPT、PPTX),假如是,则调用 handleFile 方法。
  • handleFile 方法:读取文件内容并将其添加到 fileList 中,同时触发上传。
  • handleRemove 方法:更新 fileList 以反映文件的移除。
  • beforeUpload 方法:查抄文件范例和巨细,确保只上传答应的文件范例。
  • handleSuccess 方法:处理处罚文件上传乐成后的相应,并更新 fileList。
  • removeFile 方法:从 fileList 中移除指定索引的文件。
  • isImage 方法:判断文件名是否为图片文件。
实现结果截图如下:


回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表