examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com.




var uploadFile = function () {// format for URL encoding of more charactersvar camSafeUrlEncode = function (str) {return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\\(/g, '%28').replace(/\\)/g, '%29').replace(/\\*/g, '%2A');};// Obtain the signaturevar getAuthorization = function (options, callback) {wx.request({method: 'GET',// Replace with your server address to get the signature for post uploadurl: '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: 'Failed to obtain temporary key',content: JSON.stringify(data),showCancel: false,});}},error: function (err) {wx.showModal({title: 'Failed to obtain temporary key',content: JSON.stringify(err),showCancel: false,});},});};/*** prefix: URL for COS requests* filePath: File path of the mini program selected for upload* key: Upload path for cos* formData: Authentication parameters returned by the server*/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: 'Upload succeeded', content: url, showCancel: false });} else {wx.showModal({title: 'Upload failed',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: 'Upload failed',content: JSON.stringify(res),showCancel: false,});},});requestTask.onProgressUpdate(function (res) {console.log('Current progress:', res);});};// Upload documents.var uploadFile = function (filePath) {var extIndex = filePath.lastIndexOf('.');var fileExt = extIndex >= -1 ? filePath.substr(extIndex + 1) : '';// Pass in the file extension, and the server-side generates a signed urlgetAuthorization({ ext: fileExt }, function (AuthData) {// Check whether the AuthData format is correctconsole.log(AuthData);// Parameters used in the requestvar prefix = 'https://' + AuthData.cosHost; // Request urlvar key = AuthData.cosKey; // Let the server-side determine the filename for greater security.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 });});};// Select filewx.chooseMedia({count: 1, // default 9sizeType: ['original'], // Can specify whether to use the original or compressed image. Defaults to original.sourceType: ['album', 'camera'], // Can specify source as album or camera. Defaults to both.success: function (res) {uploadFile(res.tempFiles[0].tempFilePath);},});};
var uploadFile = function () {// url encode format for encoding more charactersvar camSafeUrlEncode = function (str) {return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\\(/g, '%28').replace(/\\)/g, '%29').replace(/\\*/g, '%2A');};// Obtain the signaturevar getAuthorization = function (options, callback) {wx.request({method: 'GET',// Replace with your server address to obtain the signature for put uploadurl: '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: 'Failed to obtain temporary key',content: JSON.stringify(data),showCancel: false,});}},error: function (err) {wx.showModal({title: 'Failed to obtain temporary key',content: JSON.stringify(err),showCancel: false,});},});};/*** prefix: Request url for cos* filePath: File path of the file selected for upload in the mini program* key: Path for uploading to cos* formData: Authentication parameters returned by the server*/var putFile = function ({ prefix, filePath, key, AuthData }) {// put upload requires reading the actual content of the file for uploading.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: 'Upload succeeded',content: url,showCancel: false,});} else {wx.showModal({title: 'Upload failed',content: JSON.stringify(res),showCancel: false,});}console.log(res.statusCode);console.log(url);},fail: function fail(res) {wx.showModal({title: 'Upload failed',content: JSON.stringify(res),showCancel: false,});},});},});};// Upload documents.var uploadFile = function (filePath) {var extIndex = filePath.lastIndexOf('.');var fileExt = extIndex >= -1 ? filePath.substr(extIndex + 1) : '';getAuthorization({ ext: fileExt }, function (AuthData) {// Check whether the AuthData format is correct.console.log(AuthData);const prefix = 'https://' + AuthData.cosHost;const key = AuthData.cosKey;putFile({ prefix, filePath, key, AuthData });});};// Select filewx.chooseMedia({count: 1, // default 9sizeType: ['original'], // Can specify whether to use the original or compressed image. Defaults to original.sourceType: ['album', 'camera'], // Can specify source as album or camera. Defaults to both.success: function (res) {uploadFile(res.tempFiles[0].tempFilePath);},});};
Feedback