小程序上傳圖片到七牛云存儲(chǔ),服務(wù)端使用nodejs的thinkjs框架 服務(wù)端'use strict' import Base from './base.js'; import qiniu from 'qiniu'; export default class extends Base{ async uploadAction(){ var accessKey = '注冊(cè)七牛云之后,七牛提供的accessKey'; var secretKey = '注冊(cè)七牛云之后,七牛提供的secretKey'; var bucket = '你自己創(chuàng)建的存儲(chǔ)對(duì)象' var mac = new qiniu.auth.digest.Mac(accessKey,secretKey); var options = { scope:bucket, }; var putPolicy = new qiniu.rs.PutPolicy(options); var uploadToken = putPolicy.uploadToken(mac); think.log(uploadToken) // 給前端返回一個(gè)uploadToken return this.success(uploadToken); } } 小程序端通過(guò)服務(wù)端拿到uploadTokenupload: function () { var that = this; wx.request({ url: app.globalData.get_url + "/upload/upload", success: function (res) { var uploadToken = res.data.data; that.setData({ uploadToken: uploadToken }) } }) }, 上傳圖片chooseImage02: function (e) { var that = this; wx.chooseImage({ count: 1, sizeType: ['original', 'compressed'],// 可以指定是原圖還是壓縮圖,默認(rèn)二者都有 sourceType: ['album', 'camera'], // 可以指定來(lái)源是相冊(cè)還是相機(jī),默認(rèn)二者都有 success: function (res) { var wechatma = []; var tempFilePaths = res.tempFilePaths; var filePath = tempFilePaths[0]; //七牛提供的上傳方法 qiniuUploader.upload(filePath, (res) => { wechatma.push(res.imageURL) that.setData({ imageURL02: res.imageURL, wechatma: wechatma }); }, (error) => { console.log('error: ' + error); }, { region: 'ECN', domain: app.globalData.upload_url + '/', uptoken: that.data.uploadToken, // 由其他程序生成七牛 uptoken }); } }) }, previewImage02: function (e) { wx.previewImage({ current: e.currentTarget.id, // 當(dāng)前顯示圖片的http鏈接 urls: this.data.wechatma // 需要預(yù)覽的圖片http鏈接列表 }) }, 效果如下: |
工作日 8:30-12:00 14:30-18:00
周六及部分節(jié)假日提供值班服務(wù)