关于python:Elementui头像上传

74次阅读

共计 1777 个字符,预计需要花费 5 分钟才能阅读完成。

<template>

<div>
    <!-- action    必选参数,上传的地址  七牛云:http://up-z1.qiniu.com/-->
    <!-- data    上传时附带的额定参数 -->
    <!-- on-success    文件上传胜利时的钩子 -->
    <!-- before-upload    上传文件之前的钩子,参数为上传的文件,若返回 false 或者返回 Promise 且被 reject,则进行上传。-->
    <el-upload
        class="avatar-uploader"
        action="http://up-z1.qiniu.com/"  
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload"
        :data='postData'>
        <img v-if="imageUrl" :src="imageUrl" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
</div>

</template>
<script>
import axios from ‘axios’
export default {

data() {
    return {
        imageUrl: '',
        postData:{
            // 上传时要带上附带的 token
            token:''
        }
    }
},
methods: {
    // 获取七牛云 token
    getToken(){this.axios.get('sadmin/gettoken/').then(res=>{console.log(res.data)
            this.postData.token = res.data.token
        })
    },
    // 文件上传胜利的钩子
    handleAvatarSuccess(res, file) {this.imageUrl = [利率期货](https://www.gendan5.com/ff/if.html)'cdn 测试域名'+res.key;
        console.log(this.imageUrl)
    },
    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;
    }
},
created() {this.getToken()
}

}
</script>
<style scoped>
.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>
七牛云的存储对象的地区对应表
七牛的一张存储区域表

存储区域 区域代码 客户端上传地址 服务端上传地址
华东 ECN http(s)://upload.qiniup.com http(s)://up.qiniup.com
华北 NCN http(s)://upload-z1.qiniup.com http(s)://up-z1.qiniup.com
华南 SCN http(s)://upload-z2.qiniup.com http(s)://up-z2.qiniup.com
北美 NA http(s)://upload-na0.qiniup.com http(s)://up-na0.qiniup.com

正文完
 0