vue3中el-uplod結(jié)合ts實現(xiàn)圖片粘貼上傳
1、在確定區(qū)域添加 @paste="_handlePaste"方法;
當用戶通過瀏覽器的用戶界面發(fā)起“粘貼”動作時,將觸發(fā) paste 事件。
如果光標處于可編輯的上下文中,事件處理器可以通過調(diào)用事件 clipboardData 屬性的 getData() 方法來訪問剪貼板內(nèi)容。
<el-form :model="dialogForm" label-width="100px" :rules="rules" ref="dialogFormRef" @paste="_handlePaste">
<el-form-item label="上傳圖片" prop="imageList">
<el-upload
ref="uploadRef"
multiple
name="files"
v-model:file-list="pictureList"
:action="uploadAction"
:headers="uploadHeader"
list-type="picture-card"
:on-preview="handlePictureCardPreview"
:on-success="handlePictureSuccess"
:on-remove="handleRemove"
>
<div class="picture-upload">
<el-icon><Plus /></el-icon>
<span>上傳圖片</span>
</div>
</el-upload>
<el-dialog v-model="dialogVisible" class="image-preview">
<img w-full :src="dialogImageUrl" alt="Preview Image" />
</el-dialog>
</el-form-item>
</el-form>
2、ts部分
在頁面部分 v-model:file-list="pictureList" 使用v-model,刪除和成功上傳的方法可以不使用
import { ref, getCurrentInstance } from 'vue'
import type { UploadProps, UploadUserFile, FormRules, FormInstance, ElUpload } from 'element-plus'
import { getCookieToken } from '@/utils/util'//獲取token方法
import axios from 'axios'
const instance = getCurrentInstance() as any
const uploadAction = ref(`接口地址`)
const uploadHeader = ref({
token: getCookieToken('token')
})
const dialogForm = ref({
imageList: [], // 圖片列表
})
const pictureList = ref<UploadUserFile[]>([])// 圖片列表
const rules = reactive<FormRules>({
imageList: [{ required: true, message: '請上傳', trigger: ['blur', 'change'] }]
})
const dialogFormRef = ref<FormInstance>()
const uploadRef = ref<InstanceType<typeof ElUpload>>()
const dialogImageUrl = ref('')
const dialogVisible = ref(false)
// 圖片刪除
const handleRemove: UploadProps['onRemove'] = () => {
}
// 圖片預覽
const handlePictureCardPreview: UploadProps['onPreview'] = (uploadFile) => {
dialogImageUrl.value = uploadFile.url!
dialogVisible.value = true
}
// 上傳成功
const handlePictureSuccess:UploadProps['onSuccess'] = (e, f) => {
}
// 獲取圖片參數(shù)列表--v-model:file-list="pictureList" ,可獲取想要的圖片參數(shù)
const _getPictureList = () => {
const tempImgList = ref([]as any)
if (pictureList.value.length) {
pictureList.value.forEach((element:any) => {
if (element.response) {
tempImgList.value.push(element?.response?.data[0].url)
} else {
tempImgList.value.push(element.url)
}
})
dialogForm.value.imageList = tempImgList.value
}
}
//監(jiān)聽粘貼操作
const _handlePaste = (e:any) => {
let clipboardData = e.clipboardData
if (!clipboardData) {
// chrome
clipboardData = e.originalEvent.clipboardData
}
const items = ref(''as any)
items.value = (e.clipboardData || (window as any).clipboardData).items
const file = ref(null as any)
if (!items.value || items.value.length === 0) {
ElMessage.error('當前瀏覽器不支持粘貼本地圖片,請打開圖片復制后再粘貼!')
return
}
for (let i = 0; i < items.value.length; i++) {
// 限制上傳文件類型
if (items.value[i].type.indexOf('image') !== -1) {
file.value = items.value[i].getAsFile()
break
}
}
if (file.value) {
const fileData = reactive({
file: ''as any
})
fileData.file = file.value
_uploadfile(fileData)//調(diào)用上傳接口
}
}
//調(diào)用上傳接口
const _uploadfile = (fileObj:any) => {
const formData = new FormData()
formData.set('files', fileObj.file)
// 這里使用封裝的上傳文件的接口
axios.post('接口地址', formData, {
headers: {
token: getCookieToken('token')//獲取請求頭
}
}).then(res => {
pictureList.value.push(res.data.data[0])
instance.proxy.$forceUpdate()//強制更新
}).catch((e) => {
console.log(e, '報錯')
})
}
就可以實現(xiàn)粘貼上傳了
到此這篇關(guān)于vue3中el-uplod結(jié)合ts實現(xiàn)圖片粘貼上傳的文章就介紹到這了,更多相關(guān)vue3 ts 圖片粘貼上傳內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3.x?的shallowReactive?與?shallowRef?使用場景分析
在Vue3.x中,`shallowReactive`和`shallowRef`是用于創(chuàng)建淺層響應式數(shù)據(jù)的API,它們與`reactive`和`ref`類似,本文介紹vue3.x??shallowReactive?與?shallowRef的使用場景,感興趣的朋友一起看看吧2025-02-02
vue實現(xiàn)的上傳圖片到數(shù)據(jù)庫并顯示到頁面功能示例
這篇文章主要介紹了vue實現(xiàn)的上傳圖片到數(shù)據(jù)庫并顯示到頁面功能,結(jié)合實例形式分析了基于vue.js的數(shù)據(jù)庫操作及頁面圖片顯示相關(guān)操作技巧,需要的朋友可以參考下2018-03-03
Vue3?實現(xiàn)驗證碼倒計時功能(刷新保持狀態(tài))
倒計時的運用場景是需要經(jīng)常用到的,但是根據(jù)業(yè)務(wù)的不同,好比手機驗證碼或者是郵箱驗證碼之類的,即使用戶跳轉(zhuǎn)到其它頁面或者刷新,再次回到登錄也,驗證碼的倒計時也得保持狀態(tài),下面通過本文給大家分享Vue3?驗證碼倒計時功能實現(xiàn),感興趣的朋友一起看看吧2022-08-08
VUE對Storage的過期時間設(shè)置,及增刪改查方式
這篇文章主要介紹了VUE對Storage的過期時間設(shè)置,及增刪改查方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02

