E企盈小程序营销工具技术服务商 热线:4006-838-530

小程序批量上传图片到七牛

E企盈小程序直播系统

一、引入七牛SDK具体查看文档https://github.com/gpake/qiniu-wxapp-sdk/blob/master/README.md二、封装上传方法upload.js/** * 上传函数 */const qiniuUploader = require(“qiniuUploader”);const request = require(“./request.js”)// 初始化七牛相关参数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()传入数组,包含文件路径跟文件名,不需要自定义文件名可以修改下,只调用一次初始化方法,返回值为包含所有文件路径的数组三、调用    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,            })        }    },结果

赞(0) 打赏
未经允许不得转载:E企盈小程序开发-热线:4006-838-530 » 小程序批量上传图片到七牛
分享到: 更多 (0)
E企盈小程序直播营销卖货系统
E企盈直播平台营销卖货系统

评论 抢沙发

E企盈小程序开发

联系我们联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏