您好,登錄后才能下訂單哦!
一、引入七牛SDK
具體查看文檔
https://github.com/gpake/qiniu-wxapp-sdk/blob/master/README.md
二、封裝上傳方法upload.js
/** * 上傳函數(shù) */ const qiniuUploader = require("qiniuUploader"); const request = require("./request.js") // 初始化七牛相關(guān)參數(shù) function initQiniu() { var options = { uploadURL: 'https://up.qiniup.com', region: 'ECN', // uptokenURL: request.URL + 'qiniu-token?token=' + getApp().globalData.token, // uptoken: 'xxxx', domain: 'cdn-mpsoft.tujihome.com/', }; qiniuUploader.init(options); } function upload(filePath, success, fail) { initQiniu(); // 交給七牛上傳 qiniuUploader.upload(filePath, (res) => { success(res); }, (error) => { console.error('error: ' + JSON.stringify(error)); fail(error); }); } /** * 批量上傳 * @param images */ function batchUpload(images) { let uplist = [] for (let index in images) { const path = images[index]['url']; const key = images[index]['key']; let promise = new Promise((resolve, reject) => { qiniuUploader.upload(path, (res) => { resolve(res.imageURL) }, (error) => { reject(error) },{ uploadURL: 'https://up.qiniup.com', region: 'ECN', // uptokenURL: request.URL + 'qiniu-token?token=' + getApp().globalData.token, key: key, domain: 'cdn-mpsoft.tujihome.com/', }); }) uplist.push(promise) } Promise.all(uplist).then((resultList) => { console.log(resultList); return resultList; }, error => { console.log(error) throw err; }) } module.exports = { 'upload': upload, 'BatchUpload' : batchUpload, }
url:文件路徑,key:文件名
batchUpload()傳入數(shù)組,包含文件路徑跟文件名,不需要自定義文件名可以修改下,只調(diào)用一次初始化方法,
返回值為包含所有文件路徑的數(shù)組
三、調(diào)用
uploadImage(images) { var that = this var imageURL = []; for (var i of images) { i.key = 'gclw_' + Date.parse(new Date()) + Math.random() + '.jpg'; } try { imageURL = upload.BatchUpload(images); return imageURL; } catch (e) { wx.showModal({ title: '提示', content: e, showCancel: false, }) } },
結(jié)果
總結(jié)
以上所述是小編給大家介紹的微信小程序批量上傳圖片到七牛,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)億速云網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。