产品动态
产品公告
examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com。




var uploadFile = function () {// 对更多字符编码的 url encode 格式var camSafeUrlEncode = function (str) {return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\\(/g, '%28').replace(/\\)/g, '%29').replace(/\\*/g, '%2A');};// 获取签名var getAuthorization = function (options, callback) {wx.request({method: 'GET',// 替换为自己服务端地址 获取 post 上传签名url: 'http://127.0.0.1:3000/post-policy?ext=' + options.ext,dataType: 'json',success: function (result) {var data = result.data;if (data) {callback(data);} else {wx.showModal({title: '临时密钥获取失败',content: JSON.stringify(data),showCancel: false,});}},error: function (err) {wx.showModal({title: '临时密钥获取失败',content: JSON.stringify(err),showCancel: false,});},});};/*** prefix: 请求 cos 的 url* filePath: 小程序选择上传的文件路径* key: 上传到 cos 的路径* formData: 服务端返回的鉴权参数*/var postFile = function ({ prefix, filePath, key, formData }) {var requestTask = wx.uploadFile({url: prefix,name: 'file',filePath: filePath,formData: formData,success: function (res) {var url = prefix + '/' + camSafeUrlEncode(key).replace(/%2F/g, '/');if (res.statusCode === 200) {wx.showModal({ title: '上传成功', content: url, showCancel: false });} else {wx.showModal({title: '上传失败',content: JSON.stringify(res),showCancel: false,});}console.log(res.header['x-cos-request-id']);console.log(res.statusCode);console.log(url);},fail: function (res) {wx.showModal({title: '上传失败',content: JSON.stringify(res),showCancel: false,});},});requestTask.onProgressUpdate(function (res) {console.log('正在进度:', res);});};// 上传文件var uploadFile = function (filePath) {var extIndex = filePath.lastIndexOf('.');var fileExt = extIndex >= -1 ? filePath.substr(extIndex + 1) : '';// 传入文件后缀名,服务端生成带签名的 urlgetAuthorization({ ext: fileExt }, function (AuthData) {// 确认 AuthData 格式是否正确console.log(AuthData);// 请求用到的参数var prefix = 'https://' + AuthData.cosHost; // 请求 urlvar key = AuthData.cosKey; // 让服务端来决定文件名更安全var formData = {key: key,success_action_status: 200,'Content-Type': '','q-sign-algorithm': AuthData.qSignAlgorithm,'q-ak': AuthData.qAk,'q-key-time': AuthData.qKeyTime,'q-signature': AuthData.qSignature,policy: AuthData.policy,};if (AuthData.securityToken)formData['x-cos-security-token'] = AuthData.securityToken;postFile({ prefix, filePath, key, formData });});};// 选择文件wx.chooseMedia({count: 1, // 默认9sizeType: ['original'], // 可以指定是原图还是压缩图,这里默认用原图sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有success: function (res) {uploadFile(res.tempFiles[0].tempFilePath);},});};
var uploadFile = function () {// 对更多字符编码的 url encode 格式var camSafeUrlEncode = function (str) {return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\\(/g, '%28').replace(/\\)/g, '%29').replace(/\\*/g, '%2A');};// 获取签名var getAuthorization = function (options, callback) {wx.request({method: 'GET',// 替换为自己服务端地址,获取 put 上传签名url: 'http://127.0.0.1:3000/put-sign?ext=' + options.ext,dataType: 'json',success: function (result) {var data = result.data;if (data) {callback(data);} else {wx.showModal({title: '临时密钥获取失败',content: JSON.stringify(data),showCancel: false,});}},error: function (err) {wx.showModal({title: '临时密钥获取失败',content: JSON.stringify(err),showCancel: false,});},});};/*** prefix: 请求 cos 的 url* filePath: 小程序选择上传的文件路径* key: 上传到 cos 的路径* formData: 服务端返回的鉴权参数*/var putFile = function ({ prefix, filePath, key, AuthData }) {// put 上传需要读取文件的真实内容来上传const wxfs = wx.getFileSystemManager();wxfs.readFile({filePath: filePath,success: function (fileRes) {var requestTask = wx.request({url: prefix + '/' + key,method: 'PUT',header: {Authorization: AuthData.authorization,'x-cos-security-token': AuthData.securityToken,},data: fileRes.data,success: function success(res) {var url = prefix + '/' + camSafeUrlEncode(key).replace(/%2F/g, '/');if (res.statusCode === 200) {wx.showModal({title: '上传成功',content: url,showCancel: false,});} else {wx.showModal({title: '上传失败',content: JSON.stringify(res),showCancel: false,});}console.log(res.statusCode);console.log(url);},fail: function fail(res) {wx.showModal({title: '上传失败',content: JSON.stringify(res),showCancel: false,});},});},});};// 上传文件var uploadFile = function (filePath) {var extIndex = filePath.lastIndexOf('.');var fileExt = extIndex >= -1 ? filePath.substr(extIndex + 1) : '';getAuthorization({ ext: fileExt }, function (AuthData) {// 确认 AuthData 格式是否正确console.log(AuthData);const prefix = 'https://' + AuthData.cosHost;const key = AuthData.cosKey;putFile({ prefix, filePath, key, AuthData });});};// 选择文件wx.chooseMedia({count: 1, // 默认9sizeType: ['original'], // 可以指定是原图还是压缩图,这里默认用原图sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有success: function (res) {uploadFile(res.tempFiles[0].tempFilePath);},});};
文档反馈