UniApp基于xe-upload實現文件上傳組件

xe-upload地址:文件選擇、文件上傳組件(圖片,視頻,文件等) - DCloud 插件市場

致敬開發者!!!

感覺好用的話,給xe-upload的作者一個好評

背景:開發中經常會有上傳附件的情況,但是找了一圈沒有滿意的組件,無意間看到xe-upload,滿足我需要上傳圖片和視頻的需求,直接使用示例項目開始二次開發my-file-upload。

修改內容:

1.文件的點擊事件

2.對video類型文件的處理

3.根據文件名稱創建file文件內容

<template><view><view class="upload-wrap"><view class="cu-form-group1"><view class="label">{{label}}:</view><view class="btn-click mgb-16 upload-btn" @click="handleUploadClick" v-show="!disabled"><image :src="icons.upload" mode="aspectFill" class="upload-icon" /><text class="upload-text">上傳{{ label }}</text></view></view><view class="mgb-16 file-wrap" v-for="(item, index) in fileList" :key="index"><view class="btn-click file-line" @click="handlePreview(item)"><!-- <view class="btn-click file-line" @click="handleUploadFile(item)"> --><view class="file-info"><image :src="icons[item.fileType || 'file']" mode="aspectFill" class="file-icon" /><text class="file-name">{{ item.name || title[type] }}</text></view><image :src="icons.close" mode="aspectFill" class="file-icon"@click.stop="handleDeleteFile(index)" /></view></view><view class="mgb-16 file-wrap" v-if="fileList.length === 0 && disabled"><view class="file-line"><text class="file-empty">暫無數據</text></view></view><view><video id="myVideo" :src="videoUrl" autoplay loop muted objectFit="cover" v-if="showVideo"></video></view></view><xe-upload ref="XeUpload" :options="uploadOptions" @callback="handleUploadCallback"></xe-upload></view>
</template><script>export default {name: 'MyFileUpload',components: {},props: {type: {default: 'image', // image, video, filetype: String,},list: {default: () => ([]),type: Array,},// disabled: {// 	default: false,// 	type: Boolean,// },value: {type: String, // 或者是 File[],取決于你的需求default: null},maxFile: {type: Number, //最大上傳數量default: 1},label: {type: String, // 或者是 File[],取決于你的需求default: '附件'},},data() {return {// uploadOptions 參數跟uni.uploadFile的參數是一樣的(除了類型為Function的屬性)uploadOptions: {// url: 'http://192.168.31.185:3000/api/upload', // 不傳入上傳地址則返回本地鏈接},uploadUrl: "/sys/common/upload",staticUrl: "/sys/common/static/",fileList: [],title: {image: '圖片',video: '視頻',file: '文件',},icons: {upload: '/static/xeUpload/icon_upload.png',close: '/static/xeUpload/icon_close.png',image: '/static/xeUpload/icon_image.png',video: '/static/xeUpload/icon_video.png',file: '/static/xeUpload/icon_file.png',},disabled: false,showVideo: false,videoUrl: ''};},watch: {value: {async handler(val) {if (val && val !== null && val !== undefined) {const url = this.$config.apiUrl + this.staticUrl + val;const file = this.urlToFile(url);this.fileList = [file];if (this.fileList.length === this.maxFile) {this.disabled = true;}}},immediate: true,deep: true,},},onLoad: function() {this.videoContext = uni.createVideoContext('myVideo', this)},methods: {handleUploadClick() {// 使用默認配置則不需要傳入第二個參數// App、H5 文件拓展名過濾 { extension: ['.doc', '.docx'] } 或者 { extension: '.doc, .docx' }this.$refs.XeUpload.upload(this.type);// 可以根據當前的平臺,傳入選擇文件的參數,例如// 注意 當chooseMedia可用時,會優先使用chooseMedia// // uni.chooseImage// this.$refs.XeUpload.upload(type, {// 	count: 6,// 	sizeType: ['original', 'compressed'],// 	sourceType: ['album'],// });// // uni.chooseVideo// this.$refs.XeUpload.upload(type, {// 	sourceType: ['camera', 'album'],// });// // uni.chooseMedia (微信小程序2.10.0+;抖音小程序、飛書小程序;京東小程序支持)// this.$refs.XeUpload.upload(type, {// 	count: 9,// 	sourceType: ['album', 'camera'],// });},handleUploadCallback(e) {console.log('UploadCallback', e);if (['choose', 'success'].includes(e.type)) {// 根據接口返回修改對應的response相關的邏輯const tmpFiles = (e.data || []).map(({response,tempFilePath,name,fileType}) => {// 當前測試服務返回的數據結構如下// {//   "result": {//       "fileName": "fileName",//       "filePath": `http://192.168.1.121:3000/static/xxxxx.png`,//   },//   "success": true,// }const res = response?.result || {};const tmpUrl = res.filePath ?? tempFilePath;const tmpName = res.fileName ?? name;return {...res,url: tmpUrl,name: tmpName,fileType,};});this.fileList.push(...tmpFiles);this.handleUploadFile(e.data[0].tempFilePath);}},// 自定義上傳handleUploadFile(url) {var that = this;console.log('UploadFile', url);if (url != undefined) {that.$http.upload(that.$config.apiUrl + that.uploadUrl + '?biz=temp', {filePath: url,name: 'file',}).then(res => {console.log('handleUpload success', res);//回傳至表單that.$emit('input', res.data.message);const tmpData = JSON.parse(res.data);uni.showToast({title: tmpData.success ? '上傳成功' : '上傳失敗',icon: 'none'});})}},// 預覽handlePreview(item) {console.log('PreviewFile', item);const fileType = this.getFileType(item.name);const url = item.url;if (fileType === 'image') {return uni.previewImage({current: url,urls: [url],});}else if (fileType === 'video') {this.videoUrl = url;this.showVideo = !this.showVideo;//全屏顯示// this.videoContext.requestFullScreen();}// #ifndef H5else if (fileType === 'office') {return uni.openDocument({filePath: url,fail: (err) => {console.log(err);uni.showToast({icon: 'none',title: '文件預覽失敗'});},});}// #endifelse{uni.showModal({title: '提示',content: url,showCancel: false,});}},handleDeleteFile(index) {this.fileList.splice(index, 1);if (this.fileList.length < this.maxFile) {this.disabled = false;}},urlToFile(url) {// 獲取URL的最后一部分const lastPart = url.split('/').pop();// 獲取文件名(不包括擴展名)const filenameWithoutExtension = lastPart.split('_').slice(0, -1).join('_');// 獲取文件擴展名const extension = lastPart.split('.').pop();// 組合文件名和擴展名const filename = filenameWithoutExtension + '.' + extension;const fileType = this.getFileType(url);const file = {"fileName": filename,"fileKey": lastPart,"filePath": url,"url": url,"name": filename,"fileType": fileType}return file;},/*** 獲取文件類型* @param {String} fileName 文件鏈接* @returns {String} fileType => '', image, video, audio, office, unknown*/getFileType(fileName = '') {const fileType = fileName.split('.').pop();// let suffix = flieArr[flieArr.length - 1];// if (!suffix) return '';// suffix = suffix.toLocaleLowerCase();const image = ['png', 'jpg', 'jpeg', 'bmp', 'gif', 'webp'];if (image.includes(fileType)) return 'image';const video = ['mp4', 'm4v'];if (video.includes(fileType)) return 'video';const audio = ['mp3', 'm4a', 'wav', 'aac'];if (audio.includes(fileType)) return 'audio';const office = ['pdf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'txt', 'plain'];if (office.includes(fileType)) return 'office';return 'unknown';},},};
</script><style lang="scss" scoped>view {box-sizing: border-box;}.label{text-align: justify;padding-right: 15px;white-space: nowrap;font-size: 15px;position: relative;height: 30px;line-height: 30px;}.cu-form-group1 {background-color: #ffffff;padding: 1px 15px 1px 0px;display: flex;align-items: center;min-height: 50px;// justify-content: space-between;}.btn-click {transition: all 0.3s;opacity: 1;}.btn-click:active {opacity: 0.5;}.mgb-16 {margin-bottom: 16rpx;&:last-child {margin-bottom: 0;}}.upload-wrap {width: 100%;border-radius: 16rpx;background: white;padding: 32rpx;.upload-btn {width: 60%;height: 120rpx;border: 2rpx dashed #AAAAAA;background: #FAFAFA;border-radius: 16rpx;display: flex;align-items: center;justify-content: center;flex-direction: column;.upload-icon {width: 48rpx;height: 48rpx;margin-bottom: 8rpx;}.upload-text {font-size: 26rpx;color: #9E9E9E;line-height: 40rpx;}}.file-wrap {.file-line {width: 100%;background: #F5F5F5;border-radius: 8rpx;padding: 16rpx;font-size: 26rpx;color: #1A1A1A;line-height: 40rpx;display: flex;align-items: center;justify-content: space-between;.file-info {width: 90%;display: flex;align-items: center;.file-name {max-width: 80%;padding-left: 16rpx;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;}}.file-icon {width: 40rpx;height: 40rpx;flex-shrink: 0;}.file-empty {color: #999999;}}}}
</style>

父組件中引入使用

<my-file-upload type="file" v-model="model.attachment" label="附件"></my-file-upload>import myFileUpload from '@/components/my-componets/my-file-upload.vue';export default {name: "Test",components:{ myFileUpload },props:{formData:{type:Object,default:()=>{},required:false}},data(){return {model: {},}},onLoad: function (option) {}},created(){},methods:{}}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/75677.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/75677.shtml
英文地址,請注明出處:http://en.pswp.cn/web/75677.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

STM32 HAL庫之GPIO示例代碼

LED燈不斷閃爍 GPIO初始化&#xff0c;main文件中的 MX_GPIO_Init(); 也就是在 gpio.c文件中 void MX_GPIO_Init(void) {GPIO_InitTypeDef GPIO_InitStruct {0};/* GPIO Ports Clock Enable */__HAL_RCC_GPIOE_CLK_ENABLE();__HAL_RCC_GPIOC_CLK_ENABLE();__HAL_RCC_GPIOA_C…

二維數點 系列 題解

1.AT_dp_w Intervals 我的博客 2.CF377D Developing Games 我的博客 這兩道題是比較經典的線段樹區間 trick&#xff0c;希望自己可以在以后的比賽中手切。 3.洛谷 P10814 離線二維數點 題意 給你一個長為 n n n 的序列 a a a&#xff0c;有 m m m 次詢問&#xff0c…

vulkanscenegraph顯示傾斜模型(5.9)-vsg中vulkan資源的編譯

前言 上一章深入剖析了GPU資源內存及其管理&#xff0c;vsg中為了提高設備內存的利用率&#xff0c;同時減少內存(GPU)碎片&#xff0c;采用GPU資源內存池機制(vsg::MemoryBufferPools)管理邏輯緩存(VkBuffer)與物理內存(VkDeviceMemory)。本章將深入vsg中vulkan資源的編譯(包含…

探秘 Python 網絡編程:構建簡單聊天服務器

在計算機網絡的世界里&#xff0c;網絡編程是實現不同設備之間通信的關鍵技術。Python 憑借其簡潔的語法和強大的庫支持&#xff0c;在網絡編程領域有著廣泛的應用。無論是構建簡單的聊天服務器&#xff0c;還是開發復雜的網絡應用&#xff0c;Python 都能輕松勝任。 1 理論基礎…

Go語言Slice切片底層

Go語言&#xff08;Golang&#xff09;中切片&#xff08;slice&#xff09;的相關知識、包括切片與數組的關系、底層結構、擴容機制、以及切片在函數傳遞、截取、增刪元素、拷貝等操作中的特性。并給出了相關代碼示例和一道面試題。關鍵要點包括&#xff1a; 數組特性&#xf…

vue3 ts 自定義指令 app.directive

在 Vue 3 中&#xff0c;app.directive 是一個全局 API&#xff0c;用于注冊或獲取全局自定義指令。以下是關于 app.directive 的詳細說明和使用方法 app.directive 用于定義全局指令&#xff0c;這些指令可以用于直接操作 DOM 元素。自定義指令在 Vue 3 中非常強大&#xff0…

基于python的機器學習(五)—— 聚類(二)

一、k-medoids聚類算法 k-medoids是一種聚類算法&#xff0c;它是基于k-means聚類算法的一種改進。k-medoids算法也是一種迭代算法&#xff0c;但是它將中心點限定為數據集中的實際樣本點&#xff0c;而不是任意的點。 具體來說&#xff0c;k-medoids算法從數據集中選擇k個初…

解釋:指數加權移動平均(EWMA)

指數加權移動平均&#xff08;EWMA, Exponential Weighted Moving Average&#xff09; 是一種常用于時間序列平滑、異常檢測、過程控制等領域的統計方法。相比普通移動平均&#xff0c;它對最近的數據賦予更高權重&#xff0c;對舊數據逐漸“淡化”。 ? 一、通俗理解 想象你…

Spring Boot 項目基于責任鏈模式實現復雜接口的解耦和動態編排!

全文目錄&#xff1a; 開篇語前言一、責任鏈模式概述責任鏈模式的組成部分&#xff1a; 二、責任鏈模式的核心優勢三、使用責任鏈模式解耦復雜接口1. 定義 Handler 接口2. 實現具體的 Handler3. 創建訂單對象4. 在 Spring Boot 中使用責任鏈模式5. 配置責任鏈6. 客戶端調用 四、…

COMSOL仿真遇到的兩個小問題

最近跑熱仿真的時候跑出了兩個問題&#xff0c;上網查發現也沒什么解決方式&#xff0c;最后自己誤打誤撞的摸索著解決了&#xff0c;在這里分享一下。 問題一 我當時一準備跑仿真就彈出了這個東西&#xff0c;但在此之前從未遇到 然后我試著在它說的路徑中建立recoveries文件…

如何在英文學術寫作中正確使用標點符號?

標點符號看似微不足道&#xff0c;但它們是書面語言的無名英雄。就像熟練的指揮家指揮管弦樂隊一樣&#xff0c;標點符號可以確保您的寫作流暢、傳達正確的含義并引起讀者的共鳴。正如放錯位置的音符會在音樂中造成不和諧一樣&#xff0c;放錯位置的逗號或缺少分號也會使您的寫…

【深度學習與大模型基礎】第10章-期望、方差和協方差

一、期望 ——————————————————————————————————————————— 1. 期望是什么&#xff1f; 期望&#xff08;Expectation&#xff09;可以理解為“長期的平均值”。比如&#xff1a; 擲骰子&#xff1a;一個6面骰子的點數是1~6&#x…

JAVA虛擬機(JVM)學習

入門 什么是JVM JVM&#xff1a;Java Virtual Machine&#xff0c;Java虛擬機。 JVM是JRE(Java Runtime Environment)的一部分&#xff0c;安裝了JRE就相當于安裝了JVM&#xff0c;就可以運行Java程序了。JVM的作用&#xff1a;加載并執行Java字節碼&#xff08;.class&#…

【數據結構與算法】——堆(補充)

前言 上一篇文章講解了堆的概念和堆排序&#xff0c;本文是對堆的內容補充 主要包括&#xff1a;堆排序的時間復雜度、TOP 這里寫目錄標題 前言正文堆排序的時間復雜度TOP-K 正文 堆排序的時間復雜度 前文提到&#xff0c;利用堆的思想完成的堆排序的代碼如下&#xff08;包…

什么是柜臺債

柜臺債&#xff08;柜臺債券業務&#xff09;是指通過銀行等金融機構的營業網點或電子渠道&#xff0c;為投資者提供債券買賣、托管、結算等服務的業務模式。它允許個人、企業及機構投資者直接參與銀行間債券市場的交易&#xff0c;打破了以往僅限機構參與的壁壘。以下是綜合多…

【Android讀書筆記】讀書筆記記錄

文章目錄 一. Android開發藝術探索1. Activity的生命周期和啟動模式1.1 生命周期全面分析 一. Android開發藝術探索 1. Activity的生命周期和啟動模式 1.1 生命周期全面分析 onPause和onStop onPause后會快速調用onStop&#xff0c;極端條件下直接調用onResume 當用戶打開新…

Java對象內存結構詳解

Java對象內存結構詳解 Java對象在JVM內存中的存儲結構可以分為三個部分&#xff1a;對象頭&#xff08;Header&#xff09;、實例數據&#xff08;Instance Data&#xff09;和對齊填充&#xff08;Padding&#xff09;。以下是64位JVM&#xff08;開啟壓縮指針&#xff09;下…

【TI MSPM0】Printf重定向學習

一、新建工程 通過XDS110與電腦進行通信。 選擇這兩個引腳 需要添加這兩個頭文件 在程序中添加這三個函數即可對printf進行重定向 二、封裝函數 另一種方法 封裝一個函數&#xff0c;定義一個數組

深度強化學習基礎 0:通用學習方法

過去自己學習深度強化學習的痛點&#xff1a; 只能看到各種術語、數學公式勉強看懂&#xff0c;沒有建立清晰且準確關聯 多變量交互關系浮于表面&#xff0c;有時候連環境、代理控制的變量都混淆 模型種類繁多&#xff0c;概念繁雜難整合、對比或復用&#xff0c;無框架分析所…

asm匯編源代碼之-字庫轉換程序

將標準的16x16點陣漢字庫(下載16x16漢字庫)轉換成適合VGA文本模式下顯示的點陣漢字庫 本程序需要調用file.asm中的子程序,所以連接時需要把file連接進來,如下 C:\> tlink chghzk file 調用參數描述如下 C:\> chghzk ; 無調用參數,轉換標準庫文件(SRC16.FNT)為適合VGA…