odoo

odoo保存微信小程序上传的图片

2019-07-09  本文已影响0人  小尤root

写个小demo分享一下。后续完善
小程序端

 wx.uploadFile({
          url: 'https://youmingyu.oicp.vip/wx/shop/upload',
          filePath: path,
          name: 'wximag',
          success: res => {
            resolve(res)
          },
          fail: err=>{
            reject(err)
          }
        })
      })

odoo端

@http.route('/wx/shop/upload',auth='public', methods=['POST'],csrf=False, type='http')
    def wx_app_upload(self, wximag=None,**kwargs):
        import uuid
        import os
        base_path=os.path.abspath(os.path.dirname(__file__) + os.path.sep + "..")+'\\upload\\'
        import datetime
        file_dir = datetime.datetime.now().strftime("%Y%m%d")
        save_dir = base_path+file_dir
        if not os.path.exists(save_dir):
            os.mkdir(save_dir)
            os.chmod(save_dir,stat.S_IRWXU | stat.S_IRGRP |  stat.S_IRWXO)
        file_name = str(uuid.uuid4()).replace("-", "") + ".png"
        file_path = "{0}/{1}".format(save_dir, file_name)
        wximag.save(file_path)
        file_object = open(file_path, 'rb')
        rr = ""
        try:
            while True:
                chunk = file_object.read()
                if not chunk:
                    break
                rr = base64.b64encode(chunk)
                print rr
        finally:
            file_object.close()
        print rr
        if rr != "":
            vals = {
                "name": "小小苏哟",
                # "contact_name": kwargs.pop("customer"),
                "contact_name": "你好哦",
                # "contact_name":    "民火车",
                "phone": "12344343445",
                "add_comment": "名车e购",
                "city": 10,
                "frontimage": rr
            }
            request.env['crm.lead'].sudo().create(vals)
        os.remove(file_path)

        return "ok"

目的是要写入到数据库,所以采用保存后读取又删除。 tempfile使用失败

上一篇下一篇

猜你喜欢

热点阅读