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標(biāo)簽的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"; //七牛云上圖片文件夾和圖片名字的命名規(guī)則

wx.uploadFile({

url:'https://up-z0.qiniup.com',//分華北區(qū),華東區(qū)之類的,大家自己注意下

? ? 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);//調(diào)用服務(wù)器接口,修改用戶頭像。此處不贅述了。。。

}

},

fail:function(err) {

console.log(err)

}

});

}


//如果是一次性上傳多張圖片

七牛云上傳會報錯,error: "file exists"? 因為設(shè)置了key以后,上傳后的圖片同名了??梢园慈缦滦薷?/p>

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容