主要需要的就是file(文件本身)和token(上传凭证)。
然后在服务端部署获取上传凭证的业务代码,通过wx.request请求获取token。
var that = this; wx.request({ url: 'https://xxx/token', //从你的服务器获取 method: 'POST', data: {}, header: { 'content-type':'application/x-www-form-urlencoded' }, success: function(res) { that.token = res.data; //默认返回一个token,赋值给已经有的token属性。这里只是示例,具体根据需求可自行设定。 }, fail:function (res) { console.log(res) } })
wx.chooseImage({ count: 9, success: function (res) { var filePath = res.tempFilePaths[0]; var file_length = res.tempFilePaths.length; that.setData({ count: file_length }) for(var i = 0; i < file_length; i++){ wx.showLoading() var filePath = res.tempFilePaths[i]; // 交给七牛上传 var key = Math.random().toString(36).substr(2); //生成一个随机字符串的文件名 wx.uploadFile({ url: 'https://up.qiniup.com', //华东地区上传地址 filePath: filePath, name: 'file', formData:{ 'token': that.data.token,//刚刚获取的上传凭证 'key': key//这里是为文件设置上传后的文件名 }, success: function(r){ console.log(r) var data = r.data;//七牛会返回一个包含hash值和key的JSON字符串 if(typeof data==='string')data = JSON.parse(data.trim());//解压缩 var imglist = that.data.imglist //拼接上你的七牛云空间域名 imglist = imglist.concat('https://qiniuxxx.xiaotaitaddsdi222123.cn/' + data.key) that.setData({ imglist: imglist }); console.log(that.data.imglist) that.setData({ count: --that.data.count }) if(that.data.count <= 0){ wx.hideLoading() } }, fail:function (res) { console.log(res) } }) } } } )
有问题加我 QQ 769387485
网上 有个 sdk qiniu-wxapp-sdk 体验版上传不了 切记