在vue后臺管理系統(tǒng)里面胁勺,有時候會遇到一個圖片上傳的功能装畅,做這個功能的時候的思路是這樣的:
1:首選由前端寫一個圖片上傳的upload標簽骚露,選擇本地的圖片文件之后
2:調(diào)用后端給的接口棘捣,以二進制文件的形式傳給后端服務(wù)器
3:服務(wù)器進行處理,處理完成之后將服務(wù)器的主機名加到圖片名稱的前綴上
4:返回一個有服務(wù)器主機+圖片名稱的在線就可以訪問的鏈接給到前端
5:前端直接將在線鏈接路徑渲染在界面顯示圖片
在vue-element-admin里面的具體的實踐
1:在store/modules里面新建一個api.js
將上傳圖片至服務(wù)器的后端小伙伴給的接口寫在這個js里面
const baseUrl = 'http://39.xx.xxx.1xx/yyexploreplatform'
const state = {
fileUpload: baseUrl + '/upload/file'
}
export default { state }
2:打開store/getters.js
getters.js
const getters = {
fileUpload: state => state.api.fileUpload,
}
export default getters
3:新建一個index.vue頁面
開始寫代碼:
<template>
<div class="app-container">
<el-form ref="nextProjectForm" :model="nextProjectForm" label-width="100px">
<el-form-item label="插入圖片">
<el-upload
class="avatar-uploader"
:action="fileUpload"
:headers="{ Authorization: token }"
:show-file-list="false"
:on-success="handleAvatarSuccess">
<img
v-if="nextProjectForm.publicWelfareUrl"
:src="nextProjectForm.publicWelfareUrl"
class="avatar"/>
<i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
</el-form-item>
</el-form>
</div>
</template>
<script>
import { mapGetters } from "vuex";
export default {
data() {
return {
nextProjectForm: {
publicWelfareUrl: "",
},
};
},
computed: {
...mapGetters(["fileUpload", "token"])
},
methods: {
handleAvatarSuccess(response, file, fileList) {
if (response && response.data && response.data.url) {
this.$set(this.nextProjectForm, "publicWelfareUrl", response.data.url);
}
},
}
};
</script>
<style lang='scss'>
.avatar-uploader .el-upload {
border: 1px dashed #d9d9d9;
border-radius: 6px;
cursor: pointer;
position: relative;
overflow: hidden;
}
.avatar-uploader .el-upload:hover {
border-color: #409eff;
}
.avatar-uploader-icon {
font-size: 28px;
color: #8c939d;
width: 178px;
height: 178px;
line-height: 178px;
text-align: center;
}
.avatar {
width: 178px;
height: 178px;
display: block;
}
</style>
4:效果如下:
5:有些業(yè)務(wù)需求是這樣的宣鄙,給定一個圖片尺寸,比如說最大寬度是500默蚌,最大高度是300冻晤,如果上傳的圖片尺寸超過這個范圍,在上傳的時候绸吸,一個提示明也,不符合這個尺寸的都無法上傳。
這個時候惯裕,就需要進行判斷了
需要在upload里面添加一個:before-upload方法
然后在 methods里面寫一下這個方法的實現(xiàn)過程代碼
//對圖片大小的限制
handleImagesUrlBefore:function(file){
var _this = this;
return new Promise(function(resolve, reject) {
var reader = new FileReader();
reader.onload = function(event) {
var image = new Image();
image.onload = function () {
var width = this.width;
var height = this.height;
if (width>500 ){
_this.$alert('圖片寬度尺寸必須在500之內(nèi)温数!', '提示', {confirmButtonText: '確定'});
reject();
}
if (height >300) {
_this.$alert('圖片高度尺寸必須在300之內(nèi)!', '提示', {confirmButtonText: '確定'});
reject();
}
resolve();
};
image.src = event.target.result;
}
reader.readAsDataURL(file);
});
},
完整的參考代碼:
<template>
<div class="app-container">
<el-form ref="nextProjectForm" :model="nextProjectForm" label-width="100px">
<el-form-item label="插入圖片">
<el-upload
class="avatar-uploader"
:action="fileUpload"
:before-upload="handleImagesUrlBefore"
:headers="{ Authorization: token }"
:show-file-list="false"
:on-success="handleAvatarSuccess">
<img
v-if="nextProjectForm.publicWelfareUrl"
:src="nextProjectForm.publicWelfareUrl"
class="avatar"/>
<i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
</el-form-item>
</el-form>
</div>
</template>
<script>
import { mapGetters } from "vuex";
export default {
data() {
return {
nextProjectForm: {
publicWelfareUrl: "",
},
};
},
computed: {
...mapGetters(["fileUpload", "token"])
},
methods: {
//對圖片大小的限制
handleImagesUrlBefore:function(file){
var _this = this;
return new Promise(function(resolve, reject) {
var reader = new FileReader();
reader.onload = function(event) {
var image = new Image();
image.onload = function () {
var width = this.width;
var height = this.height;
if (width>500 ){
_this.$alert('圖片寬度尺寸必須在500之內(nèi)蜻势!', '提示', {confirmButtonText: '確定'});
reject();
}
if (height >300) {
_this.$alert('圖片高度尺寸必須在300之內(nèi)撑刺!', '提示', {confirmButtonText: '確定'});
reject();
}
resolve();
};
image.src = event.target.result;
}
reader.readAsDataURL(file);
});
},
handleAvatarSuccess(response, file, fileList) {
if (response && response.data && response.data.url) {
this.$set(this.nextProjectForm, "publicWelfareUrl", response.data.url);
}
},
}
};
</script>
<style lang='scss'>
.avatar-uploader .el-upload {
border: 1px dashed #d9d9d9;
border-radius: 6px;
cursor: pointer;
position: relative;
overflow: hidden;
}
.avatar-uploader .el-upload:hover {
border-color: #409eff;
}
.avatar-uploader-icon {
font-size: 28px;
color: #8c939d;
width: 178px;
height: 178px;
line-height: 178px;
text-align: center;
}
.avatar {
width: 178px;
height: 178px;
display: block;
}
</style>
如果不符合尺寸的時候,會有一個彈框進行提示握玛。