国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Vue實(shí)現(xiàn)附件上傳功能

瀏覽:41日期:2023-01-18 10:30:12

本文實(shí)例為大家分享了Vue實(shí)現(xiàn)附件上傳的具體代碼,供大家參考,具體內(nèi)容如下

前言

前端 UI 是用的是 element-ui 的上傳功能

本文主要記錄下代碼,方便下次復(fù)制粘貼

前端部分

HTML

limit: 限制文件個(gè)數(shù) 1 個(gè) on-remove: 移除附件時(shí)的鉤子函數(shù),主要就 console 輸出下 on-error: 用于處理上傳異常后的處理,本人這主要用來(lái)關(guān)閉彈窗和全屏等待 file-list: 綁定附件 auto-upload: 禁止自動(dòng)上傳,true 的話選了文件就自動(dòng)上傳 http-request: 自定義上傳文件請(qǐng)求方法,默認(rèn)方法會(huì)與 mock 產(chǎn)生 XmlRequest 重新生成導(dǎo)致找不到文件問題,我注釋了 mock 還是那樣,沒具體研究 action: 原上傳文件的路徑,由于使用了自定義上傳文件請(qǐng)求,即 http-request,因此這個(gè)字段隨便寫就好,不寫不行好像

<el-upload ref='upload' :limit='1' :on-remove='handleRemove' :on-error='onError' :file-list='fileList' :auto-upload='false' :http-request='uploadFile' action='https://jsonplaceholder.typicode.com/posts/' class='upload-demo'> <el-button slot='trigger' size='small' type='primary'>選取文件</el-button> <!-- <el-button size='small' type='success' @click='submitUpload'>上傳到服務(wù)器</el-button> --> <div slot='tip' class='el-upload__tip'>支持上傳 {{ strRebuild(fileType) }} 格式,且不超過 {{ fileSize }}M</div></el-upload>

JS

import { strRebuild, lastSubstring } from ’@/utils/strUtil’import { message } from ’@/utils/message’export default { data() { return { // 附件列表 fileList: [], // 允許的文件類型 fileType: [’xls’, ’xlsx’, ’pdf’, ’doc’, ’docx’, ’txt’, ’jpg’, ’png’, ’jpeg’], // 運(yùn)行上傳文件大小,單位 M fileSize: 10, } }, methods: { // 清空表單 clear() { // 清空附件 this.$refs.upload.clearFiles() }, // 附件檢查 // 檢查附件是否屬于可上傳類型 // 檢查附件是否超過限制大小 checkFile() { var flag = true var tip = ’’ var files = this.$refs.upload.uploadFiles files.forEach(item => { // 文件過大 if (item.size > this.fileSize * 1024 * 1024) { flag = false tip = ’ 文件超過’ + this.fileSize + ’M’ } // 文件類型不屬于可上傳的類型 if (!this.fileType.includes(lastSubstring(item.name, ’.’))) { flag = false tip = ’ 文件類型不可上傳’ } }) if (!flag) { message(’error’, tip) } return flag }, // 提交附件 submitUpload() { if (this.checkFile()) { console.log(’上傳附件...’) this.$refs.upload.submit() } else { console.log(’取消上傳’) } }, // 自定義文件上傳方法 uploadFile(file) { // 把文件放入 FormData 進(jìn)行提交 const param = new FormData() param.append(’files’, file.file) uploadFile(param).then(response => { // TODO 一些關(guān)閉彈框,上傳成功提示等 }) }, // 移除附件 handleRemove(file, fileList) { console.log(’移除附件...’) }, // 附件上傳失敗,打印下失敗原因 onError(err) { message(’error’, ’附件上傳失敗’) console.log(err) }, // 字符串重組 strRebuild(str) { return strRebuild(str) } }}

工具類 JS

strUtil.js

// 字符串相關(guān)工具類// 數(shù)組根據(jù)分隔符重組為字符串export function strRebuild(arr, split) { if (arr === undefined || arr === null || !(arr instanceof Array) || arr.length === 0) { return ’’ } if (split === undefined || split === null) { split = ’,’ } var str = ’’ arr.forEach((v, i) => { if (i === arr.length - 1) { str = str + v } else { str = str + v + split } }) return str}// 截取最后一個(gè)特定字符后面的字符串export function lastSubstring(str, split) { if (str === undefined || str === null || split === undefined || split === null) { return ’’ } return str.substring(str.lastIndexOf(split) + 1)}

message.js

import { Message } from ’element-ui’// 提示封裝 type 提示類型, msg 提示信息,duration 持續(xù)時(shí)間export function message(type, msg, duration) { Message({ message: msg || ’success’, type: type || ’success’, duration: duration || 5 * 1000 })}// 帶刪除鍵提示,duration 為 0 時(shí),不會(huì)自動(dòng)消失// 提示封裝 type 提示類型, msg 提示信息,duration 持續(xù)時(shí)間export function messageShowClose(type, msg, duration) { Message({ message: msg || ’success’, type: type || ’success’, duration: duration || 0, showClose: true })}

API

// 附件上傳export function uploadFile(file) { return request({ url: ’/uploadFile’, method: ’post’, headers: { ’Content-Type’: ’multipart/form-data; charset=utf-8’ }, data: file })}

后端接口

/** * 單文件上傳 * @param files 接收文件要以數(shù)組接收 * @return */@PostMapping(value='/uploadFile')public void uploadFile(@RequestBody MultipartFile[] files) { // TODO}

更多文章可以點(diǎn)擊《Vue.js前端組件學(xué)習(xí)教程》學(xué)習(xí)閱讀。

關(guān)于vue.js組件的教程,請(qǐng)大家點(diǎn)擊專題vue.js組件學(xué)習(xí)教程進(jìn)行學(xué)習(xí)。

更多vue學(xué)習(xí)教程請(qǐng)閱讀專題《vue實(shí)戰(zhàn)教程》

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Vue
相關(guān)文章:
主站蜘蛛池模板: 新兴县| 吉隆县| 新昌县| 久治县| 长岭县| 商河县| 隆林| 乌拉特中旗| 平南县| 荣昌县| 肥东县| 广州市| 鸡西市| 奎屯市| 邵阳市| 澄江县| 横山县| 闸北区| 寻乌县| 扶余县| 天峻县| 陇西县| 巩留县| 随州市| 碌曲县| 全南县| 噶尔县| 大理市| 马鞍山市| 景谷| 遂川县| 西华县| 邹城市| 南岸区| 三河市| 桂林市| 津市市| 玉屏| 朔州市| 苗栗县| 万全县|