vue3+uniapp 上傳附件的操作代碼
uni-file-picker搭配uni.uploadFile在使用問題上踩了不少坑,我至今還是沒辦法在不改uniapp源碼基礎(chǔ)上實(shí)現(xiàn)限制重復(fù)文件的上傳。因?yàn)槭冀K怎么限制,uni-file-picker綁定的v-model的值fileList,好像只要上傳了文件展示就一定會(huì)回顯那一個(gè)附件。還有一個(gè)問題,就是上傳的進(jìn)度條并不能正常工作。直接css隱藏算了眼不見心不煩
多次試圖馴服該組件,發(fā)現(xiàn)如果要把上傳好的文件列表同步給父組件,就不要去傳組件綁定的value值,因?yàn)檫@會(huì)出問題,把這個(gè)變量當(dāng)做單純展示的作用就好了,父子組件的數(shù)據(jù)傳輸,可以用雙向綁定,文件的增刪改都操作這個(gè)雙向綁定的數(shù)據(jù)。
子組件:封裝的文件上傳
c-upload.vue
<template> <uni-file-picker v-model="fileList" :auto-upload="false" file-mediatype="all" mode="grid" @delete="delFile" @select="select" > <text class="iconfont icon-zengjiatianjiajiahao"></text> </uni-file-picker> </template> <script setup> import {ref, reactive, watch} from "vue"; const props = defineProps({ appendixEntityList: {default: []}, }); const emit = defineEmits(["update:appendixEntityList"]); let fileList = ref([]); // 選擇上傳觸發(fā)函數(shù) const select = (e) => { // 根據(jù)所選圖片的個(gè)數(shù),多次調(diào)用上傳函數(shù) let promises = []; for (let i = 0; i < e.tempFilePaths.length; i++) { const promise = uploadFiles(e.tempFilePaths, i); promises.push(promise); } Promise.all(promises).then(() => { }); }; // 上傳函數(shù) const uploadFiles = async (tempFilePaths, i) => { await uni.uploadFile({ url: '/api/xxx/xxxx/xxxx', //后端用于處理圖片并返回圖片地址的接口 filePath: tempFilePaths[i], name: "file", header: { Authorization: "Bearer " + uni.getStorageSync("token") || "", ContentType: "application/x-www-form-urlencoded", skipToken: true, }, success: (res) => { let v = JSON.parse(res.data); //返回的是字符串,需要轉(zhuǎn)成對(duì)象格式 if (v.code == 200) { props.appendixEntityList.push({ appendixId: v.data.id, appendixOriginal: v.data.original, appendixUrl: v.data.attachUrl, }); emit("update:appendixEntityList", props.appendixEntityList); } }, fail: () => { console.log("err"); }, }); }; //文件列表回顯 const fileListEcho = () => { if (props.appendixEntityList.length > 0) { fileList.value = []; props.appendixEntityList?.forEach((v) => { //改成官方文檔要求的格式。才能回顯附件 fileList.value.push({ name: v?.appendixOriginal, extname: v?.appendixType, url: v?.appendixUrl, }); }); } }; // 移出圖片函數(shù) const delFile = async (val) => { //在提交數(shù)組中刪除那個(gè)被刪的文件 props.appendixEntityList.some((v, i) => { if (v.appendixOriginal === val.tempFile.name) { props.appendixEntityList.splice(i, 1); emit("update:appendixEntityList", props.appendixEntityList); return true; } }); }; watch( () => props.appendixEntityList, (newVal) => { fileListEcho(); }, ); </script> <style lang="scss" scoped> .icon-zengjiatianjiajiahao { font-size: 42rpx; color: #a2a3a5; position: absolute; right: 0; top: -50rpx; } </style>
父組件:
<!-- 上傳--> <c-upload ref="uploadRef" v-model:appendixEntityList="form.appendixEntityList" ></c-upload>
到此這篇關(guān)于vue3+uniapp 上傳附件的文章就介紹到這了,更多相關(guān)vue3+uniapp 上傳附件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Vue3.0開發(fā)輕量級(jí)手機(jī)端彈框組件V3Popup的場(chǎng)景分析
這篇文章主要介紹了基于Vue3.0開發(fā)輕量級(jí)手機(jī)端彈框組件V3Popup,本文通過場(chǎng)景分析給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12使用Vite+Vue3+TypeScript?搭建開發(fā)腳手架的詳細(xì)過程
這篇文章主要介紹了Vite+Vue3+TypeScript?搭建開發(fā)腳手架的詳細(xì)過程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-02-02laravel5.3 vue 實(shí)現(xiàn)收藏夾功能實(shí)例詳解
這篇文章主要介紹了laravel5.3 vue 實(shí)現(xiàn)收藏夾功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2018-01-01Vue 前端實(shí)現(xiàn)登陸攔截及axios 攔截器的使用
這篇文章主要介紹了Vue 前端實(shí)現(xiàn)登陸攔截及axios 攔截器的使用,通過這個(gè)項(xiàng)目學(xué)習(xí)如何實(shí)現(xiàn)一個(gè)前端項(xiàng)目中所需要的 登錄及攔截、登出、token失效的攔截及對(duì)應(yīng) axios 攔截器的使用。需要的朋友可以參考下2019-07-07基于express中路由規(guī)則及獲取請(qǐng)求參數(shù)的方法
下面小編就為大家分享一篇基于express中路由規(guī)則及獲取請(qǐng)求參數(shù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-03-03vue中如何給多個(gè)按鈕動(dòng)態(tài)添加類名
這篇文章主要介紹了vue中如何給多個(gè)按鈕動(dòng)態(tài)添加類名問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11