element-ui提供了文件上传的组件,官方文档。官方文档还是比较详细的。这里是一点实际应用。这里用了头像上传和照片墙
官方示例
<script>
export default {
name: 'AdminComponentsUpload',
data () {
return {
imageUrl: ''
}
},
created () {},
methods: {
beforeAvatarUpload (file) {
const isJPG = file.type === 'image/jpeg'
const isLt2M = file.size / 1024 / 1024 < 2
if (!isJPG) {
this.$message.error('上传头像图片只能是 JPG 格式!')
}
if (!isLt2M) {
this.$message.error('上传头像图片大小不能超过 2MB!')
}
return isJPG && isLt2M
},
handleAvatarSuccess (res, file) {
this.imageUrl = URL.createObjectURL(file.raw)
},
handleRemove (file, fileList) {
console.log(file, fileList)
},
handlePictureCardPreview (file) {
console.log(file)
}
}
}
</script>
<template>
<el-main>
<el-scrollbar class="default-scrollbar" wrap-class="default-scrollbar__wrap" view-class="p20-scrollbar__view">
<h2 class="title">用户头像上传</h2>
<div class="avater-wrap">
<el-upload
class="avatar-uploader"
action="https://jsonplaceholder.typicode.com/posts/"
:show-file-list="false"
:on-success="handleAvatarSuccess"
:before-upload="beforeAvatarUpload">
<img v-if="imageUrl" :src="imageUrl" class="avatar">
<i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
</div>
<h2 class="title">照片墙</h2>
<div class="wall-wrap">
<el-upload
action="https://jsonplaceholder.typicode.com/posts/"
list-type="picture-card"
:on-preview="handlePictureCardPreview"
:on-remove="handleRemove">
<i class="el-icon-plus"></i>
</el-upload>
</div>
</el-scrollbar>
</el-main>
</template>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.title {
line-height: 60px;
}
.avater-wrap {
width: 180px;
height: 180px;
}
.avatar-uploader {
border: 1px dashed #d9d9d9;
border-radius: 6px;
cursor: pointer;
position: relative;
overflow: hidden;
}
.avatar-uploader: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>
第一个问题:上传图片的时候跨域错误。这个问题就不在这里解决了,实际项目中不会用这个地址上传。
第二个问题:换成自己的服务器地址,vue项目中我用的是axios进行ajax请求。所有的ajax都在/src/request中统一管理。还有服务器的接口是要带access_token才允许访问上传图片的接口的。
假如我的上传请求如下:
// IMAGE api
import Service from '../base'
export default {
uploadImage (file, handleProgress) {
const formData = new FormData()
formData.append('file', file)
return Service.post('/zuul/image/upload', formData, {
progress (e) {
if (e.total > 0) {
e.percent = e.loaded / e.total * 100
}
if (handleProgress) {
handleProgress(e)
}
},
onUploadProgress (e) {
if (e.total > 0) {
e.percent = e.loaded / e.total * 100
}
if (handleProgress) {
handleProgress(e)
}
}
})
}
}
access_token在base中统一处理。这样就要修改组件的参数了。
- action的值改成'upload',不使用组件的这个参数上传文件;
- 增加参数:http-request="handleUploadImage"。用自己的方法进行上传。这个回调函数中实现上传业务;
- 实现handleUploadImage函数
handleUploadImage (options) {
return new Promise((resolve, reject) => {
FileService.uploadImage(options.file, options.onProgress).then(res => {
resolve(res)
}).catch(error => {
this.$message.error(error || '上传图片失败!')
})
})
}
经过这样修改,上传的过程可以在FileService.uploadImage中控制,上传后结果返回到on-success回调的第一个参数中。进度会传回FileService.uploadImage第二个参数指向的函数中。
最终代码
<script>
import FileService from '@/request/file/File'
export default {
name: 'AdminComponentsUpload',
data () {
return {
imageUrl: ''
}
},
created () {},
methods: {
handleUploadImage (options) {
return new Promise((resolve, reject) => {
FileService.uploadImage(options.file, options.onProgress).then(res => {
resolve(res)
}).catch(error => {
this.$message.error(error || '上传图片失败!')
})
})
},
beforeAvatarUpload (file) {
const isJPG = file.type === 'image/jpeg'
const isLt2M = file.size / 1024 / 1024 < 2
if (!isJPG) {
this.$message.error('上传头像图片只能是 JPG 格式!')
}
if (!isLt2M) {
this.$message.error('上传头像图片大小不能超过 2MB!')
}
return isJPG && isLt2M
},
handleAvatarSuccess (res, file) {
this.imageUrl = URL.createObjectURL(file.raw)
},
handleRemove (file, fileList) {
console.log(file, fileList)
},
handlePictureCardPreview (file) {
console.log(file)
}
}
}
</script>
<template>
<el-main>
<el-scrollbar class="default-scrollbar" wrap-class="default-scrollbar__wrap" view-class="p20-scrollbar__view">
<h2 class="title">用户头像上传</h2>
<div class="avater-wrap">
<el-upload
class="avatar-uploader"
action="upload"
:show-file-list="false"
:http-request="handleUploadImage"
:on-success="handleAvatarSuccess"
:before-upload="beforeAvatarUpload">
<img v-if="imageUrl" :src="imageUrl" class="avatar">
<i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
</div>
<h2 class="title">照片墙</h2>
<div class="wall-wrap">
<el-upload
action="upload"
list-type="picture-card"
:http-request="handleUploadImage"
:on-preview="handlePictureCardPreview"
:on-remove="handleRemove">
<i class="el-icon-plus"></i>
</el-upload>
</div>
</el-scrollbar>
</el-main>
</template>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.title {
line-height: 60px;
}
.avater-wrap {
width: 180px;
height: 180px;
}
.avatar-uploader {
border: 1px dashed #d9d9d9;
border-radius: 6px;
cursor: pointer;
position: relative;
overflow: hidden;
}
.avatar-uploader: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>