js本地压缩图片,上传base64给后台,再给七牛

来源:互联网 发布:单片机开发板能做什么 编辑:程序博客网 时间:2024/04/26 14:34

首先是js本地压缩。参考:

http://www.cnblogs.com/baie/p/3618676.html

前台是Joy写的。


压缩后产生了base64。

不知道如何base64直传七牛。



所以只好先转给后台。

以form 表单的形式post。

看了下正则指南:

http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html

然后后台正则处理:

data = request.form['data']  

# 正则提取。求更好的表达式 add by lee @14.9.21 18:45  

rdata = re.findall(r';base64,(.+)',data)[0]



把base64转二进制,用七牛python SDK上传。
        data =base64.b64decode(rdata)
        # 因为是服务器传七牛,遂取消回调,所以g.user.id其实意义不大
        uptoken, key = update(config.AVATAR_BUCKET , '', '', g.user.id, '', 'square100' )
        import qiniu.io
        ret, err = qiniu.io.put(uptoken, key, data)


后台保存图片地址,并修改用户role。

        g.user.avatar_path = "http://"+config.AVATAR_BUCKET+".qiniudn.com/" + key
        # 上传真实照片,可转变role。切记,只是0变成1,别管理员变成1了。  add by lee 14.9.12
        if g.user.role == 0:
            g.user.role = 1
        print g.user.avatar_path
        db.session.add(g.user)
        db.session.commit()


对了,update函数也做了修改。原先只是针对预处理的。

现在加入不处理适配:

############ 将上传封装-以后可能用到: 头像、背景、相册 add by lee @14.8.22############## 如果是本地压缩,七牛不处理图片的话:# 只需写: update(bucket,'','',g.user.id,'',pipeline)def update(orgin_bucket, target_bucket, returnUrl, user_id, oops, pipeline):    conf.ACCESS_KEY = 'xxxxxxxxxxxxxxxxxxx'    conf.SECRET_KEY = 'xxxxxxxxxxxxxxxxxxx'    key = str(datetime.now())[:19]+str(user_id)    policy = rs.PutPolicy("%s:%s" % (orgin_bucket, key))    policy.returnUrl = returnUrl    policy.returnBody = '{"hash": $(etag), "key": "%s", "persistentId":"$(persistentId)"}'%key    policy.mimeLimit = "image/*"    policy.fsizeLimit = 5000000    # 本地压缩后上传和原图直传,上传策略不一样  add by lee @14.9.21 21:28    if oops == '':        entry = target_bucket+':'+ key        EncodeEntryURI = base64.urlsafe_b64encode(entry)        # oops 举例:'imageView2/1/w/100/h/100/q/100|saveas/'        Ops = oops + EncodeEntryURI        policy.persistentOps = Ops    policy.persistentPipeline = pipeline    uptoken = policy.token()    return uptoken, key



0 0
原创粉丝点击