examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com
。cos.ap-guangzhou.myqcloud.com
。具体可参考下文代码中的注释。POST http://examplebucket-1250000000.cos.ap-beijing.myqcloud.com/
,请求的域名是存储桶域名。这样如果在小程序里用到多个存储桶,则需要配置这个存储桶域名作为白名单域名。解决方法如下:POST http://cos.ap-beijing.myqcloud.com/examplebucket-1250000000/
,请求的域名是地域域名,存储桶名称放在请求的路径里。在小程序里用到同一个地域多个存储桶,只需要配置一个域名cos.ap-beijing.myqcloud.com
作为白名单域名。/examplebucket-1250000000/
。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,});},});};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) : '';getAuthorization({ ext: fileExt }, function (AuthData) {// 请求用到的参数var prefix = 'https://' + AuthData.cosHost;var 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,});},});};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,});},});requestTask.onProgressUpdate(function (res) {console.log('正在进度:', res);});},});};// 上传文件var uploadFile = function (filePath) {var extIndex = filePath.lastIndexOf('.');var fileExt = extIndex >= -1 ? filePath.substr(extIndex + 1) : '';getAuthorization({ ext: fileExt }, function (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);},});};
本页内容是否解决了您的问题?