美文网首页
vue 七牛云base64上传

vue 七牛云base64上传

作者: 大梦无痕 | 来源:发表于2021-03-11 17:59 被阅读0次
          methods:{
                  //七牛云base64上传
                base64(pic){
                    var than = this;
                    //pic base64字符串
                    this.getQiniuToken({}).then((res)=>{
                        if(res.code==0){
                            var token = res.data;
                            pic = pic.replace(/^.*?,/,'');
                            var url = "http://upload.qiniu.com/putb64/"+this.fileSize(pic)+"/"+window.btoa("0612_"+ new Date().getTime()+"_"+parseInt(Math.random()*10)); //非华东空间需要根
                            var xhr = new XMLHttpRequest();
                            xhr.onreadystatechange=function(){
                                if (xhr.readyState==4){
                                    //上传成功返回的数据
                                    var res = JSON.parse(xhr.responseText);
                                    console.log(res );
                                }
                            }
                            xhr.open("POST", url, true);
                            xhr.setRequestHeader("Content-Type", "application/octet-stream");
                            //此处UpToken 后面带空格
                            xhr.setRequestHeader("Authorization", "UpToken "+token);
                            xhr.send(pic);
                        }
                    });
                    
                },
                fileSize(str){
                    if (str.indexOf('=') > 0) {
                        var indexOf = str.indexOf('=');
                        str = str.substring(0,indexOf);
                    }
                    return parseInt(str.length - (str.length / 8) * 2)
                },
          }
    
    image.png

    相关文章

      网友评论

          本文标题:vue 七牛云base64上传

          本文链接:https://www.haomeiwen.com/subject/hvdoqltx.html