Vue實(shí)現(xiàn)附件上傳功能
本文實(shí)例為大家分享了Vue實(shí)現(xiàn)附件上傳的具體代碼,供大家參考,具體內(nèi)容如下
前言
前端 UI 是用的是 element-ui 的上傳功能
本文主要記錄下代碼,方便下次復(fù)制粘貼
前端部分
HTML
- limit: 限制文件個(gè)數(shù) 1 個(gè)
- on-remove: 移除附件時(shí)的鉤子函數(shù),主要就 console 輸出下
- on-error: 用于處理上傳異常后的處理,本人這主要用來關(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 style="margin-left: 10px;" 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)教程》
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue從后臺(tái)渲染文章列表以及根據(jù)id跳轉(zhuǎn)文章詳情詳解
這篇文章主要給大家介紹了關(guān)于vue從后臺(tái)渲染文章列表以及根據(jù)id跳轉(zhuǎn)文章詳情的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
Element中使用ECharts的項(xiàng)目實(shí)踐
本文主要介紹了Element中使用ECharts的項(xiàng)目實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
Vue實(shí)現(xiàn)點(diǎn)擊導(dǎo)航欄當(dāng)前標(biāo)簽后變色功能
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)點(diǎn)擊導(dǎo)航欄當(dāng)前標(biāo)簽后變色功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08
vue+element-ui表格自定義列模版的實(shí)現(xiàn)
本文主要介紹了vue+element-ui表格自定義列模版的實(shí)現(xiàn),通過插槽完美解決了element-ui表格自定義列模版的問題,具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05

