图片上传(ImageCropper)
此前端代码自己封装了文件上传,只需要配置后端接口需求URL以及对应的图片上传成功后的处理函数,后端返回OSS生成的图片访问地址,然后cropsuccess函数将上传成功的图像进行显示。
<template>
<div class="app-container">
<el-form-item label="讲师头像">
<pan-thumb :image="teacher.avatar"/>
<el-button type="primary" icon="el-icon-upload"
@click="imagecropperShow=true">更换头像
</el-button>
<image-cropper
v-show="imagecropperShow"
:width="300"
:height="300"
:key="imagecropperKey"
:url="BASE_API+'/eduoss/fileoss'"
field="file"
@close="close"
@crop-upload-success="cropSuccess"/>
</el-form-item>
</div>
</template>
<script>
import ImageCropper from '@/components/ImageCropper'
import PanThumb from '@/components/PanThumb'
export default{
components: { ImageCropper, PanThumb },
data() {
return{
teacher:{},
saveBtnDisabled:false,
imagecropperKey:0,
imagecropperShow:false,
BASE_API:process.env.BASE_API,
saveBtnDisabled:false
}
},
methods: {
close() {
this.imagecropperShow=false
this.imagecropperKey = this.imagecropperKey+1
},
cropSuccess(data) {
this.imagecropperShow=false
this.teacher.avatar = data.url
this.imagecropperKey = this.imagecropperKey+1
},
}
}
</script>
文件上传(el-upload)
<template>
<div class="app-container">
<el-form label-width="120px">
<el-form-item label="信息描述">
<el-tag type="info">excel模版说明</el-tag>
<el-tag>
<i class="el-icon-download"/>
<a :href="'/static/01.xlsx'">点击下载模版</a>
</el-tag>
</el-form-item>
<el-form-item label="选择Excel">
<el-upload
ref="upload"
:auto-upload="false"
:on-success="fileUploadSuccess"
:on-error="fileUploadError"
:disabled="importBtnDisabled"
:limit="1"
:action="BASE_API+'/eduservice/subject/addSubject'"
name="file"
accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet">
<el-button slot="trigger" size="small" type="primary">选取文件</el-button>
<el-button
:loading="loading"
style="margin-left: 10px;"
size="small"
type="success"
@click="submitUpload">上传到服务器</el-button>
</el-upload>
</el-form-item>
</el-form>
</div>
</template>
<script>
export default {
data() {
return {
BASE_API: process.env.BASE_API,
importBtnDisabled: false,
loading: false
}
},
created() {
},
methods:{
submitUpload() {
this.importBtnDisabled = true
this.loading = true
this.$refs.upload.submit()
},
fileUploadSuccess(response) {
this.loading = false
this.$message({
type: 'success',
message: '添加课程分类成功'
})
this.$router.push({path:'/subject/list'})
},
fileUploadError() {
this.loading = false
this.$message({
type: 'error',
message: '添加课程分类失败'
})
}
}
}
</script>
注意
name属性值要与后端接口参数MultipartFile的变量名一致,否则无法映射匹配传值。 前端标识符属性值和后端参数名称(实体类中属性名)保持一致,否则无法直接映射传参,导致后端接收不到数据。
|