• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

关于python:Elementui头像上传

python 搞代码 3年前 (2022-02-20) 26次浏览 已收录 0个评论

<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

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:关于python:Elementui头像上传
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址