python+vue+element-ui文件上传

本文介绍了前端使用Element UI实现头像上传,通过Canvas处理图片并转化为Base64,以及后端如何接收Base64数据,解码存储到服务器的过程。涉及技术包括HTML、CSS、JavaScript和Node.js的文件操作。
摘要由CSDN通过智能技术生成

 前端:

<el-upload 
    class="avatar-uploader"
    action="#"
    :show-file-list="false" 
    :auto-upload="false" 
    :on-change="updateEdit"
    :before-upload="beforeAvatarUpload">
        <el-button style="margin-left: 10px" size="small" type="success">
        <i class="el-icon-upload"></i>更换头像</el-button>
</el-upload>
    const updateEdit = (file: any) => {
      const image = new Image()
      image.src = URL.createObjectURL(file.raw)
      image.onload = async () => {
        const canvas = document.createElement('canvas')
        canvas.width = image.width
        canvas.height = image.height
        const context = canvas.getContext('2d')
        context.drawImage(image, 0, 0, image.width, image.height)
        updheaderimg.header_img = canvas.toDataURL() //图片的base64地址
        const res = await Service.Updheaderimg({ ...updheaderimg })
        getcurrentuser()
      }
      updheaderimg.filename = file.name
      updheaderimg.uid = file.uid
    }

 后端:

STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)
try:
   header_img=b['header_img']
   b64_data = header_img.split(';base64,')[1]
   car_image = base64.b64decode(b64_data)
   suffix = b['filename'].split('.')[1]
   picName = str(b['uid']) + '.' + suffix
   path = os.path.join(settings.BASE_DIR, r"static\headerImage\%s" %(picName))
   file = open(path, 'wb')
   file.write(car_image)
   file.close()
   UserModel.objects.filter(id=id).update(header_img=picName)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值