vue3+uniapp 上傳附件的操作代碼
uni-file-picker搭配uni.uploadFile在使用問題上踩了不少坑,我至今還是沒辦法在不改uniapp源碼基礎上實現(xiàn)限制重復文件的上傳。因為始終怎么限制,uni-file-picker綁定的v-model的值fileList,好像只要上傳了文件展示就一定會回顯那一個附件。還有一個問題,就是上傳的進度條并不能正常工作。直接css隱藏算了眼不見心不煩
多次試圖馴服該組件,發(fā)現(xiàn)如果要把上傳好的文件列表同步給父組件,就不要去傳組件綁定的value值,因為這會出問題,把這個變量當做單純展示的作用就好了,父子組件的數(shù)據(jù)傳輸,可以用雙向綁定,文件的增刪改都操作這個雙向綁定的數(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ù)所選圖片的個數(shù),多次調用上傳函數(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); //返回的是字符串,需要轉成對象格式
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ù)組中刪除那個被刪的文件
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>到此這篇關于vue3+uniapp 上傳附件的文章就介紹到這了,更多相關vue3+uniapp 上傳附件內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
基于Vue3.0開發(fā)輕量級手機端彈框組件V3Popup的場景分析
這篇文章主要介紹了基于Vue3.0開發(fā)輕量級手機端彈框組件V3Popup,本文通過場景分析給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12
使用Vite+Vue3+TypeScript?搭建開發(fā)腳手架的詳細過程
這篇文章主要介紹了Vite+Vue3+TypeScript?搭建開發(fā)腳手架的詳細過程,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-02-02
laravel5.3 vue 實現(xiàn)收藏夾功能實例詳解
這篇文章主要介紹了laravel5.3 vue 實現(xiàn)收藏夾功能,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2018-01-01
Vue 前端實現(xiàn)登陸攔截及axios 攔截器的使用
這篇文章主要介紹了Vue 前端實現(xiàn)登陸攔截及axios 攔截器的使用,通過這個項目學習如何實現(xiàn)一個前端項目中所需要的 登錄及攔截、登出、token失效的攔截及對應 axios 攔截器的使用。需要的朋友可以參考下2019-07-07
基于express中路由規(guī)則及獲取請求參數(shù)的方法
下面小編就為大家分享一篇基于express中路由規(guī)則及獲取請求參數(shù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03

