国产亚洲精品福利在线无卡一,国产精久久一区二区三区,亚洲精品无码国模,精品久久久久久无码专区不卡

當前位置: 首頁 > news >正文

瑞麗網站建設深圳整站seo

瑞麗網站建設,深圳整站seo,做網站的公司合肥,常見網站建設公司術語要在 Element UI 的拖拽上傳組件中實現(xiàn) Ctrl V 圖片上傳功能,可以通過監(jiān)聽鍵盤事件來捕獲粘貼操作,并將粘貼的圖片數(shù)據(jù)上傳到服務器。 版本V1,實現(xiàn)獲取粘貼板中的文件 注意,本案例需要再你已經安裝了Element UI并在項目中正確配…

要在 Element UI 的拖拽上傳組件中實現(xiàn) Ctrl + V 圖片上傳功能,可以通過監(jiān)聽鍵盤事件來捕獲粘貼操作,并將粘貼的圖片數(shù)據(jù)上傳到服務器。

版本V1,實現(xiàn)獲取粘貼板中的文件

注意,本案例需要再你已經安裝了Element UI并在項目中正確配置的情況下進行,第一個版本僅適合上傳jpeg和png的圖片

創(chuàng)建拖拽上傳組件
假設你已經有一個基本的拖拽上傳組件,我們可以在此基礎上添加 Ctrl + V 功能。

監(jiān)聽粘貼事件
我們需要在頁面中監(jiān)聽 paste 事件,當用戶按下 Ctrl + V 時,捕獲粘貼板中的圖片數(shù)據(jù)。

處理粘貼事件

在捕獲到圖片數(shù)據(jù)后,將其轉換為 File 對象,并調用上傳方法。

代碼如下:

<template><div><el-uploaddragaction="https://jsonplaceholder.typicode.com/posts/":on-preview="handlePreview":on-remove="handleRemove":before-upload="beforeUpload"multipleref="upload"><i class="el-icon-upload"></i><div class="el-upload__text">將文件拖到此處,或<em>點擊上傳</em></div><div class="el-upload__tip" slot="tip">只能上傳jpg/png文件,且不超過500kb</div></el-upload></div>
</template>
<script>
import { Upload } from 'element-ui';export default {name: 'DragUpload',methods: {handlePaste(event) {// 捕獲粘貼事件const items = event.clipboardData.items;for (let i = 0; i < items.length; i++) {if (items[i].type.indexOf('image') !== -1) {// 獲取圖片文件const file = items[i].getAsFile();this.handleFile(file);break;}}},handleFile(file) {// 將文件添加到上傳隊列this.$refs.upload.handleStart(file);this.$refs.upload.submit();},handlePreview(file) {console.log('Preview:', file);},handleRemove(file, fileList) {console.log('Remove:', file, fileList);},beforeUpload(file) {const isJPGorPNG = file.type === 'image/jpeg' || file.type === 'image/png';const isLt500K = file.size / 1024 < 500;if (!isJPGorPNG) {this.$message.error('只能上傳 JPG/PNG 格式的圖片!');}if (!isLt500K) {this.$message.error('圖片大小不能超過 500KB!');}return isJPGorPNG && isLt500K;}},mounted() {// 監(jiān)聽粘貼事件document.addEventListener('paste', this.handlePaste);},beforeDestroy() {// 移除粘貼事件監(jiān)聽document.removeEventListener('paste', this.handlePaste);}
};
</script>
  1. HTML部分:使用 el-upload 組件創(chuàng)建一個拖拽上傳區(qū)域。
  2. JavaScript部分:
    • handlePaste 方法:捕獲粘貼事件,檢查粘貼板中的數(shù)據(jù)是否為圖片文件,如果是,則調用 handleFile 方法。
    • handleFile 方法:將圖片文件添加到上傳隊列,并提交上傳。
    • mounted 生命周期鉤子:添加粘貼事件監(jiān)聽器。
    • beforeDestroy 生命周期鉤子:移除粘貼事件監(jiān)聽器,防止內存泄漏。

隨便截圖一張,我們這個時候ctrl + v 就可以發(fā)現(xiàn)他可以獲取我們粘貼板中的文件。

在這里插入圖片描述
在這里插入圖片描述
我們到這一步發(fā)現(xiàn),圖片網頁是獲取到。這個時候你在跟著你的業(yè)務,傳遞相關參數(shù),這第V1版本就可以用了。

第二版本V2,可以直接在粘貼的過程在下面以壓縮圖片的形式展示圖片

<template><div><el-uploaddrag:action="uploadFileUrl":on-preview="handlePreview":on-remove="handleRemove":before-upload="beforeUpload":on-success="handleSuccess"multipleref="upload":file-list="fileList"><i class="el-icon-upload"></i><div class="el-upload__text">將文件拖到此處,或<em>點擊上傳</em></div><div class="el-upload__tip" slot="tip">只能上傳jpg/png文件,且不超過500kb</div></el-upload><!-- 顯示上傳后的文件 --><div v-for="(file, index) in fileList" :key="index" class="uploaded-file"><div v-if="isImage(file.name)"><img :src="file.url" alt="Uploaded Image" class="uploaded-image" /><el-button type="text" @click="removeFile(index)">移除</el-button></div><div v-else><span>{{ file.name }}</span><el-button type="text" @click="removeFile(index)">移除</el-button></div></div></div>
</template>
<script>
import { Upload } from 'element-ui';export default {name: 'DragUpload',data() {return {fileList: []};},methods: {handlePaste(event) {const items = event.clipboardData.items;for (let i = 0; i < items.length; i++) {if (items[i].type.indexOf('image') !== -1) {const file = items[i].getAsFile();this.handleFile(file);break;}}},handleFile(file) {const reader = new FileReader();reader.onload = (e) => {this.fileList.push({name: file.name,url: e.target.result});};reader.readAsDataURL(file);this.$refs.upload.handleStart(file);this.$refs.upload.submit();},handlePreview(file) {console.log('Preview:', file);},handleRemove(file, fileList) {this.fileList = fileList;},beforeUpload(file) {const isJPGorPNG = file.type === 'image/jpeg' || file.type === 'image/png';const isLt500K = file.size / 1024 < 500;if (!isJPGorPNG) {this.$message.error('只能上傳 JPG/PNG 格式的圖片!');}if (!isLt500K) {this.$message.error('圖片大小不能超過 500KB!');}return isJPGorPNG && isLt500K;},handleSuccess(response, file, fileList) {// 更新 fileListthis.fileList = fileList.map(f => ({name: f.name,url: f.url || f.response.url // 假設服務器返回的響應中有 url 字段}));},removeFile(index) {this.fileList.splice(index, 1);},isImage(fileName) {return fileName.toLowerCase().endsWith('.jpg') || fileName.toLowerCase().endsWith('.png');}},mounted() {document.addEventListener('paste', this.handlePaste);},beforeDestroy() {document.removeEventListener('paste', this.handlePaste);}
};
</script>
<style scoped>
.uploaded-file {margin-top: 10px;display: flex;align-items: center;
}.uploaded-image {max-width: 100px;max-height: 100px;margin-right: 10px;
}
</style>

在這里插入圖片描述

在這里插入圖片描述
如圖所示。Ctrl + V就實現(xiàn)到了這一步。這里有問題,那就是你看一下,點擊上傳后的圖片是否會顯示出來呢?

http://m.aloenet.com.cn/news/31494.html

相關文章:

  • 湖南做網站 真好磐石網絡東莞公司網上推廣
  • 網站建設銷售發(fā)展前景百度指數(shù)關鍵詞搜索趨勢
  • 英文seo公司seo文章
  • 網站域名使用期怎么去推廣自己的店鋪
  • 做ar的網站搜資源的搜索引擎
  • 婦聯(lián)網站建設方案搜索歷史記錄
  • 熟人做網站怎么收錢湖南seo服務電話
  • 東昌網站建設網站建設策劃書案例
  • 做網站用的云控制臺活動推廣軟文范例
  • 重慶有什么好玩的旅游景點寧波seo外包優(yōu)化
  • 濟南制作網站的公司哪家好汕頭網站建設優(yōu)化
  • 網站被墻301怎么做付費推廣平臺有哪些
  • 科威網絡做網站怎么樣html網頁制作app
  • 網站建設合同是否繳納印花稅中國網站排名查詢
  • 金寨縣建設規(guī)劃局網站信息流廣告有哪些投放平臺
  • 做夾具需要知道的幾個網站企業(yè)網站seo方案
  • 中億豐建設集團股份有限公司網站百度競價是什么工作
  • 學做餅干網站發(fā)稿網
  • 行業(yè)門戶網站模板中國剛剛發(fā)生8件大事
  • 如何開發(fā)app小程序win優(yōu)化大師
  • 北京市文化局政務網站建設項目獨立站seo怎么做
  • 深圳網站制作鄭州怎么優(yōu)化網站排名靠前
  • 南通seo公司網站免費推廣產品平臺有哪些
  • 上海奉賢 網站建設百度指數(shù)查詢移動版
  • 廉江網站建設公眾號推廣合作平臺
  • html網頁設計基礎seo優(yōu)化主要做什么
  • 網站開發(fā)者模式企業(yè)官網建站
  • 學網站建設需要什么軟件百度外包公司有哪些
  • 企業(yè)網站建設參考資料競價推廣賬戶競價托管
  • 珠海服務好的網站建設武漢seo