demo-post-policy.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. var config = require('./config');
  2. var uploadFile = function () {
  3. // 请求用到的参数
  4. // var prefix = 'https://cos.' + config.Region + '.myqcloud.com/' + config.Bucket + '/'; // 这个是后缀式,签名也要指定 Pathname: '/' + config.Bucket + '/'
  5. var prefix = 'https://' + config.Bucket + '.cos.' + config.Region + '.myqcloud.com/';
  6. // 对更多字符编码的 url encode 格式
  7. var camSafeUrlEncode = function (str) {
  8. return encodeURIComponent(str)
  9. .replace(/!/g, '%21')
  10. .replace(/'/g, '%27')
  11. .replace(/\(/g, '%28')
  12. .replace(/\)/g, '%29')
  13. .replace(/\*/g, '%2A');
  14. };
  15. // 获取临时密钥
  16. var getCredentials = function (options, callback) {
  17. wx.request({
  18. method: 'GET',
  19. url: 'http://127.0.0.1:3000/post-policy?key=' + encodeURIComponent(options.Key), // 服务端签名,参考 server 目录下的两个签名例子
  20. dataType: 'json',
  21. success: function (result) {
  22. var data = result.data;
  23. if (data) {
  24. callback(data);
  25. } else {
  26. wx.showModal({title: '临时密钥获取失败', content: JSON.stringify(data), showCancel: false});
  27. }
  28. },
  29. error: function (err) {
  30. wx.showModal({title: '临时密钥获取失败', content: JSON.stringify(err), showCancel: false});
  31. }
  32. });
  33. };
  34. // 上传文件
  35. var uploadFile = function (filePath) {
  36. var Key = filePath.substr(filePath.lastIndexOf('/') + 1); // 这里指定上传的文件名
  37. getCredentials({Key: Key}, function (credentials) {
  38. var formData = {
  39. 'key': Key,
  40. 'success_action_status': 200,
  41. 'Content-Type': '',
  42. 'q-sign-algorithm': credentials.qSignAlgorithm,
  43. 'q-ak': credentials.qAk,
  44. 'q-key-time': credentials.qKeyTime,
  45. 'q-signature': credentials.qSignature,
  46. 'policy': credentials.policy,
  47. };
  48. if (credentials.securityToken) formData['x-cos-security-token'] = credentials.securityToken;
  49. var requestTask = wx.uploadFile({
  50. url: prefix,
  51. name: 'file',
  52. filePath: filePath,
  53. formData: formData,
  54. success: function (res) {
  55. var url = prefix + camSafeUrlEncode(Key).replace(/%2F/g, '/');
  56. if (res.statusCode === 200) {
  57. wx.showModal({title: '上传成功', content: url, showCancel: false});
  58. } else {
  59. wx.showModal({title: '上传失败', content: JSON.stringify(res), showCancel: false});
  60. }
  61. console.log(res.statusCode);
  62. console.log(url);
  63. },
  64. fail: function (res) {
  65. wx.showModal({title: '上传失败', content: JSON.stringify(res), showCancel: false});
  66. }
  67. });
  68. requestTask.onProgressUpdate(function (res) {
  69. console.log('正在进度:', res);
  70. });
  71. });
  72. };
  73. // 选择文件
  74. wx.chooseImage({
  75. count: 1, // 默认9
  76. sizeType: ['original'], // 可以指定是原图还是压缩图,这里默认用原图
  77. sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
  78. success: function (res) {
  79. uploadFile(res.tempFiles[0].path);
  80. }
  81. })
  82. };
  83. module.exports = uploadFile;