Taro 微信小程序,直接上传至七牛云

var qiniuUploader = require('../../libs/qiniuUploader.js');

//获取七牛云token

getTokenFn = () =>{

this.props.getQiniuyunTokenFn({}).then(res => {

console.log(res);

qiniuData=res;

}).catch((err) => {

})

}

//选择头像图片

chooseAvat = () => {

let that=this;

Taro.chooseImage({

count:1,

sizeType: ['original','compressed'],

sourceType: ['album','camera'],

success(res) {

// tempFilePath可以作为img标签的src属性显示图片

      const tempFilePaths = res.tempFilePaths;

that.uploadToQiniu(tempFilePaths[0]);

},

fail(err){

}

})

}


//图片直接上传到七牛云,获取七牛云链接

uploadToQiniu=(filePath)=>{

let that=this,photoResultUrl='';

let qiniu_key ='userphoto/'+ Date.parse(new Date()) /1000 +".jpg"; //七牛云上图片文件夹和图片名字的命名规则

wx.uploadFile({

url:'https://up-z0.qiniup.com',//分华北区,华东区之类的,大家自己注意下

    name:'file',

filePath: filePath,

formData: {

token:qiniuData.uptoken,

key:qiniu_key

    },

success:function(res) {

let data = res.data;

if(typeof data==='string'){

data = JSON.parse(data.trim());//解压缩

        photoResultUrl=qiniuData.domain+''+data.key +'?imageView2/0/w/110';  //七牛云图片的高级图片处理,此处是图片瘦身,支持jpg和png

that.changePhoto(photoResultUrl);//调用服务器接口,修改用户头像。此处不赘述了。。。

}

},

fail:function(err) {

console.log(err)

}

});

}


//如果是一次性上传多张图片

七牛云上传会报错,error: "file exists"  因为设置了key以后,上传后的图片同名了。可以按如下修改

let qiniu_key ='usersuggest/'+ filePath.substr(20,30)+'.jpg';

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容