vue3?頭像上傳?組件功能實現(xiàn)
更新時間:2023年05月18日 08:23:19 作者:ps酷教程
這篇文章主要介紹了vue3頭像上傳組件功能,用到了自定義組件v-model的雙向綁定,使用axios + formData 上傳文件,本文結(jié)合實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
vue3 頭像上傳 組件功能
- 用到了
自定義組件v-model的雙向綁定
- 使用
input的type=file這個原生html元
素,通過監(jiān)聽change事件
,獲取到選擇的文件(注意,選擇完文件值后,要把這個隱藏的input的type=file元素的value置為空
,否則,下次選擇同樣的圖片,將不會觸發(fā)change事件) - 使用
axios + formData 上傳文件
- 后臺做保存文件,以及
靜態(tài)資源目錄映射
即可
前端
AvatarUpload.vue
<template> <div class="avatar-upload-wrapper"> <!-- {{ modelValue }} --> <div :class="['avatar-box',{'avatar-box-border':imgUrl?false:true}]" @click="clickAvatarBox" :style="{width: size + 'px',height: size + 'px'}"> <!-- 隱藏的input的type=file --> <input type="file" hidden ref="fileInputRef" accept="image/x-png,image/gif,image/jpeg,image/bmp" @change="changeFile"> <img v-if="imgUrl" :src="imgUrl" alt=""> <div v-else class="avatar-marker"> <i class="iconfont icon-jiahao"></i> </div> </div> </div> </template> <script setup> import Messager from '@/utils/messager' import axiosInstance from '@/utils/request' import { ref,reactive,watch } from 'vue' const emits = defineEmits(['update:modelValue']) const props = defineProps({ size: { type: Number, default: 64 }, modelValue: { type: String, default: '' // 默認(rèn)頭像鏈接地址 }, maxSize: { type:Number, default: 5 // 默認(rèn)最大不超過5M }, serverUrl: { type:String, default: 'http://localhost:9091/static/img' } }) const fileInputRef =ref(null) // const imgUrl = ref('http://localhost:9091/static/img/avatar/3026520210706112210298.png') const imgUrl = ref(props.modelValue) // console.log(imgUrl.value); // 監(jiān)聽頭像url改變(打開彈框時, 傳入的圖片地址變化時, 同步修改imgUrl) watch(()=>props.modelValue,(newVal,oldVal)=>{ imgUrl.value = newVal }) function clickAvatarBox() { fileInputRef.value.click() } function changeFile() { console.log(123,fileInputRef.value.files[0].size); // 獲取更改后的文件 let file = fileInputRef.value.files[0] // 校驗文件大小 if(file.size / 1024 / 1024 > props.maxsize) { Messager.error('文件超過指定大小') } // 執(zhí)行文件上傳 let formData = new FormData() formData.append("mfile", file) formData.append("type", "avatar") let config = { headers: { 'Content-Type': 'multipart/form-data', 'a':'b' // 隨便自己帶個什么請求頭 } } // 這個config可以不必攜帶, 當(dāng)使用FormData傳參時, // axios會自己帶上'Content-Type': 'multipart/form-data',請求頭 axiosInstance.post('/file/uploadFile',formData,config ).then(res=>{ console.log(res,'上傳成功'); imgUrl.value = props.serverUrl + res let img = new Image() img.src = imgUrl.value img.onload = ()=>{ emits('update:modelValue', imgUrl.value) } }) } </script> <style lang="scss" scoped> .avatar-box-border { border: 1px dashed #409eff !important; } .avatar-box { border-radius: 50%; margin-left: 20px; cursor: pointer; position: relative; border: 2px solid #eee; overflow: hidden; &:hover::before { content:''; display: block; position: absolute; width: 100%; height: 100%; background: rgba(0,0,0,.03); } img { width: 100%; height: 100%; object-fit: cover; } .avatar-marker { position: absolute; width: 100%; height: 100%; display: flex; align-items: center; justify-content: center; color: #409eff; i.iconfont { font-size: 24px; } } } </style>
使用AvatarUpload.vue
<el-dialog v-model="userDialogVisible" width="450"> <el-form :model="userForm" :rules="userFormRules" label-width="80"> <el-form-item label="昵稱" prop="nickname"> <el-input v-model="userForm.nickname" style="width: 300px;"></el-input> </el-form-item> <!-- 使用頭像上傳組件 --> <el-form-item label="頭像" prop="avatar"> <avatar-upload v-model="userForm.avatar" /> </el-form-item> <el-form-item label="個性簽名" prop="bio"> <el-scrollbar> <el-input type="textarea" :rows="3" v-model="userForm.bio" style="width: 300px;"></el-input> </el-scrollbar> </el-form-item> <el-form-item label="網(wǎng)站鏈接" prop="website"> <el-input v-model="userForm.website" style="width: 300px;"></el-input> </el-form-item> <el-form-item label="是否可用" prop="disabled"> <el-switch v-model="userForm.disabled" :active-value="0" :inactive-value="1" active-color="#13ce66" inactive-color="#eaecf0"> </el-switch> </el-form-item> <el-form-item> <div style="margin-left: auto;"> <el-button @click="userDialogVisible = false">取消</el-button> <el-button type="primary" @click="handleSave">確定</el-button> </div> </el-form-item> </el-form> </el-dialog>
后端
上傳接口
@PostMapping("uploadFile") public Result<String> uploadFile(@RequestParam("mfile") MultipartFile mfile, String type) { return Result.ok(fileService.saveFile(mfile,type)); } @Override public String saveFile(MultipartFile mfile, String type) { String filePath = FilePathEnum.type(type).getPathPrefix() + SnowflakeIdWorker.generateId().substring(0, 8) + mfile.getOriginalFilename(); String targetFilePath = fileSavePath + filePath; try { mfile.transferTo(new File(targetFilePath)); } catch (IOException e) { throw BizException.SAVE_FILE_ERR; } return filePath; }
配置mvc
@Configuration public class WebMvcConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/static/**") .addResourceLocations("file:D:\\Projects\\boot-blog\\src\\main\\resources\\static\\"); } }
到此這篇關(guān)于vue3 頭像上傳 組件的文章就介紹到這了,更多相關(guān)vue3 頭像上傳 組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue中JS動畫與Velocity.js的結(jié)合使用
這篇文章主要介紹了Vue中JS動畫與Velocity.js的結(jié)合使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02Vue.js 通過jQuery ajax獲取數(shù)據(jù)實現(xiàn)更新后重新渲染頁面的方法
今天小編小編就為大家分享一篇Vue.js 通過jQuery ajax獲取數(shù)據(jù)實現(xiàn)更新后重新渲染頁面的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08Elementui如何限制el-input框輸入小數(shù)點
這篇文章主要介紹了Elementui如何限制el-input框輸入小數(shù)點,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08Vue elementui字體圖標(biāo)顯示問題解決方案
這篇文章主要介紹了Vue elementui字體圖標(biāo)顯示問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08