<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>
<el-button type="primary" plain v-on:click="upData">上传</el-button>
data() {
return {
imageUrl: '',
file:''
};
},
handleAvatarSuccess(res, file) {
this.imageUrl = URL.createObjectURL(file.raw);
//图片路径
},
beforeAvatarUpload(file) {
this.file = file;
console.log(this.file)
},
upData(){
let formData = new FormData();
formData.append('head', this.file);
//第一个参数是键 第二个是值
//然后在这里发送请求 如果还有其他参数 继续append
}
**
注意:请求头为:
**
headers: {
'Content-Type': 'multipart/form-data', // 默认值
},
head就是图片文件