您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關vue中vantUI如何實現文件上傳功能的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
說在前面
網上有很多種文件上傳的方法,根據公司最近的業務需求,要求實現多種文件的上傳,在實現過程中,查閱了很多資料,最后,終于把功能實現了,開心!
<template> <div> <v-header :left-text="`上傳${columnName}`"></v-header> <div class="upload"> <div v-if="columnName === '視頻'"> <div class="forPreview_video" v-for="(item, index) in uploadVideoList" :key="index"> <video :src="videoSrc"></video> <van-icon name="delete" @click="delBtn(index)" class="delte"/> </div> <van-uploader v-show="uploadVideoList.length < 2" preview-size="80px" accept="video/*" :before-read="beforeRead" :after-read="afterRead"></van-uploader> </div> <div v-if="columnName === '文檔'"> <div class="forPreview_doc" v-for="(item, index) in uploadDocList" :key="index"> <img src="../../assets/img/resource_doc_b@2x.png" alt=""> <span>{{item.name}}</span> <span>{{item.size | formatSize}}</span> <van-icon name="delete" @click="delBtn(index)" class="delte"/> </div> <van-uploader v-show="uploadDocList.length < 2" preview-size="80px" accept=".doc, .docx, .xml, .xlsx, .pdf" :before-read="beforeRead" :after-read="afterRead"></van-uploader> </div> <div v-if="columnName === '音頻'"> <div class="forPreview_audio" v-for="(item, index) in uploadAudioList" :key="index"> <img src="../../assets/img/resource_audio@2x.png" alt=""> <span>{{item.name}}</span> <span>{{item.size | formatSize}}</span> <van-icon name="delete" @click="delBtn(index)" class="delte"/> </div> <van-uploader v-show="uploadAudioList.length < 2" preview-size="80px" accept="audio/*" :before-read="beforeRead" :after-read="afterRead"></van-uploader> </div> <div v-if="columnName === '圖片'"> <div class="forPreview_pic" v-for="(item, index) in uploadPicList" :key="index"> <img :src="picSrc" alt=""> <van-icon name="delete" @click="delBtn(index)" class="delte"/> </div> <van-uploader v-show="uploadPicList.length < 2" accept="image/*" preview-size="80px" :before-read="beforeRead" :after-read="afterRead"></van-uploader> </div> <div class="diy-submit"> <van-button @click="doSubmit($event)">提交</van-button> </div> </div> </div> </template>
上述html代碼是在同一頁面根據前一頁面傳回的columnName的值來判斷需要上傳哪種類型文件。
<script> import VHeader from '../../common/header' import {classifyList, uploadFile, addResources} from '../../http/mock' import Toast from 'vant' export default { name: "uploadFile", components: { VHeader }, data(){ return{ tagList: [], uploadTitle: '', currentTag: null, tagId: null, columnName: localStorage.getItem('columnName'), fileIdArr: [], uploadVideoList: [], // 選中的上傳視頻列表 videoSrc: '', // 選中的視頻的src,用來顯示視頻 uploadDocList: [], // 選中的上傳文檔列表 uploadAudioList: [], // 選中的上傳音頻列表 uploadPicList: [], // 選中的上傳圖片列表 picSrc: '', // 選中的圖片的src,用來顯示圖片縮略圖 } }, filters: { formatSize(val) { // 格式化文件大小 if (val > 0) { return (val / 1024 / 1024).toFixed(2) + 'M'; } else { return '0M'; } }, }, methods: { // vant上傳文件前校驗文件事件 // 文件選中后先提交給后臺,后臺根據選中的文件,返回數組(這一業務根據后臺而定) beforeRead(file){ let formData = new FormData(); // 為上傳文件定義一個formData對象 let config = { headers: { 'Content-Type': 'multipart/form-data' } }; let uploadUrl = URL.createObjectURL(file); // 將選中的上傳文件轉化為二進制文件,顯示在頁面上 if(this.columnName === '視頻'){ this.uploadVideoList.push(file); this.videoSrc = uploadUrl; // 這里使用foreach是為了將選中的多個文件都添加進定義的formdata變量中 this.uploadVideoList.forEach(item => { formData.append(item.name, item) }) }else if(this.columnName === '文檔'){ this.uploadDocList.push(file); this.uploadDocList.forEach(item => { formData.append(item.name, item) }) }else if(this.columnName === '音頻'){ this.uploadAudioList.push(file); this.uploadAudioList.forEach(item => { formData.append(item.name, item) }) }else if(this.columnName === '圖片'){ this.uploadPicList.push(file); this.picSrc = uploadUrl; this.uploadPicList.forEach(item => { formData.append(item.name, item) }) } // formData.append(file.name, file); // 單個文件上傳時可以這么寫,上面的foreach就可以刪掉 this.$api.post(uploadFile, formData, config).then(res => { this.fileIdArr = res.data.data; // 把選中的文件傳送給後臺 }).catch(err => { Toast('文件上傳失敗!') }) }, // 刪除待上傳的文件 delBtn(index){ if(this.columnName === '視頻'){ // 先判斷當前的選中的索引是否是在有效范圍中,如果不是則跳出方法 if(isNaN(index) || index >= this.uploadVideoList.length){ return false; } let tmp = []; // 將沒被選中的上傳文件存放進一個臨時數組中 for(let i = 0; i < this.uploadVideoList.length; i++){ if(this.uploadVideoList[i] !== this.uploadVideoList[index]){ tmp.push(this.uploadVideoList[i]); } } // 存放當前未被選中的上傳文件 this.uploadVideoList = tmp; } if(this.columnName === '文檔'){ console.log(this.uploadDocList.length) if(isNaN(index) || index >= this.uploadDocList.length){ return false; } let tmp = []; for(let i = 0; i < this.uploadDocList.length; i++){ if(this.uploadDocList[i] !== this.uploadDocList[index]){ tmp.push(this.uploadDocList[i]); } } this.uploadDocList = tmp; } if(this.columnName === '音頻'){ if(isNaN(index) || index >= this.uploadAudioList.length){ return false; } let tmp = []; for(let i = 0; i < this.uploadAudioList.length; i++){ if(this.uploadAudioList[i] !== this.uploadAudioList[index]){ tmp.push(this.uploadAudioList[i]); } } this.uploadAudioList = tmp; } if(this.columnName === '圖片'){ if(isNaN(index) || index >= this.uploadPicList.length){ return false; } console.log('uuu') let tmp = []; for(let i = 0; i < this.uploadPicList.length; i++){ if(this.uploadPicList[i] !== this.uploadPicList[index]){ tmp.push(this.uploadPicList[i]); } } this.uploadPicList = tmp; } }, doSubmit(){ let params = { classify: this.tagId, // 針對視頻資源時對應的分類id file: this.fileIdArr, // 選擇完文件后,調用uploadFile這個接口,后端返回的數組 resourceColumnId: JSON.parse(localStorage.getItem('columnId')), // 資源欄目id(視頻、圖片、音頻、文檔) title: this.uploadTitle // 上傳時填寫的標題 }; let columnName = localStorage.getItem('columnName') this.$api.post(addResources, params).then(res => { if(res.data.code === 1001){ if(columnName === '視頻'){ this.$router.push({name: 'myVideo'}); }else { this.$router.push({name: 'myResourceClassify'}); } } }).catch(err => { console.log(err) }) }, }, mounted(){ this.getClassifyList(); } } </script> <style lang="less" scoped> .upload{ padding: 140px 36px 160px 36px; box-sizing: border-box; } .forPreview_video{ position: relative; /*background: rgba(0,0,0,.7);*/ video{ width: 95%; max-height: 430px; } .delte{ position: absolute; right: 0; bottom: 0; } } .forPreview_doc, .forPreview_audio{ display: flex; margin-bottom: 10px; align-items: center; img{ width: 56px; height: 56px; margin-right: 20px; } span:nth-of-type(1){ flex: 1; } span:nth-of-type(2){ margin-right: 20px; } } .forPreview_pic{ display: flex; align-items: flex-end; img{ width: 160px; height: 160px; } } .diy-detail{ width: 100%; overflow: hidden; .btn{ span{ margin-bottom: 10px; } } .van-cell{ background-color: #F0F0F0; border-radius: 35px; font-size: 26px; height: 69px; line-height: 69px; padding: 0 22px; color: #999; } .van-hairline--top-bottom::after, .van-hairline-unset--top-bottom::after { border-width: 0; } p{ height: 64px; line-height: 64px; font-size: 32px; color: #333; position: relative; padding-left: 16px; } p::before{ position: absolute; top: 0; left: 0; content: '*'; color: #FF0000; } span{ display: inline-block; width: 157px; background: #F0F0F0; border-radius: 35px; color: #999; font-size: 26px; padding: 14px 18px; margin-right: 28px; text-align: center; } .active{ color: #fff; background: linear-gradient(to right,#FD5130,#FA6C34); } } .diy-submit { position: fixed; height: 150px; width: 90%; bottom: 0; background: #fff; .van-button { width: 100%; height: 90px; border-radius: 45px; font-size: 36px; color: #fff; background: linear-gradient(to right, #FD5130, #FA6C34); top: 50%; transform: translate(0, -50%); } .van-button--default { border: none; } } </style>
感謝各位的閱讀!關于“vue中vantUI如何實現文件上傳功能”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。