sts-form.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Form 表单简单上传</title>
  6. <style>h1, h2 {font-weight: normal;}#msg {margin-top:10px;}</style>
  7. </head>
  8. <body>
  9. <h1>Form 表单简单上传(兼容 IE8)</h1>
  10. <div>最低兼容到 IE6 上传,不支持 onprogress</div>
  11. <form id="form" target="submitTarget" action="" method="post" enctype="multipart/form-data" accept="*/*">
  12. <input id="name" name="name" type="hidden" value="">
  13. <input name="success_action_status" type="hidden" value="200">
  14. <input id="success_action_redirect" name="success_action_redirect" type="hidden" value="">
  15. <input id="key" name="key" type="hidden" value="">
  16. <input id="Signature" name="Signature" type="hidden" value="">
  17. <input name="Content-Type" type="hidden" value="">
  18. <input id="x-cos-security-token" name="x-cos-security-token" type="hidden" value="">
  19. <input id="fileSelector" name="file" type="file">
  20. <input id="submitBtn" type="button" value="提交">
  21. </form>
  22. <iframe id="submitTarget" name="submitTarget" style="display:none;" frameborder="0"></iframe>
  23. <div id="msg"></div>
  24. <script src="common/cos-auth.min.js"></script>
  25. <script>
  26. (function () {
  27. // 请求用到的参数
  28. var Bucket = 'test-1250000000';
  29. var Region = 'ap-guangzhou';
  30. var protocol = location.protocol === 'https:' ? 'https:' : 'http:';
  31. var prefix = protocol + '//' + Bucket + '.cos.' + Region + '.myqcloud.com/';
  32. var form = document.getElementById('form');
  33. form.action = prefix;
  34. // 对更多字符编码的 url encode 格式
  35. var camSafeUrlEncode = function (str) {
  36. return encodeURIComponent(str)
  37. .replace(/!/g, '%21')
  38. .replace(/'/g, '%27')
  39. .replace(/\(/g, '%28')
  40. .replace(/\)/g, '%29')
  41. .replace(/\*/g, '%2A');
  42. };
  43. // 计算签名
  44. var getAuthorization = function (options, callback) {
  45. // var url = 'http://127.0.0.1:3000/sts';
  46. var url = '../server/sts.php';
  47. var xhr = new XMLHttpRequest();
  48. xhr.open('GET', url, true);
  49. xhr.onreadystatechange = function (e) {
  50. if (xhr.readyState === 4) {
  51. if (xhr.status === 200) {
  52. var credentials;
  53. try {
  54. credentials = (new Function('return ' + xhr.responseText))().credentials;
  55. } catch (e) {}
  56. if (credentials) {
  57. callback(null, {
  58. XCosSecurityToken: credentials.sessionToken,
  59. Authorization: CosAuth({
  60. SecretId: credentials.tmpSecretId,
  61. SecretKey: credentials.tmpSecretKey,
  62. Method: options.Method,
  63. Pathname: options.Pathname,
  64. })
  65. });
  66. } else {
  67. console.error(xhr.responseText);
  68. callback('获取签名出错');
  69. }
  70. } else {
  71. callback('获取签名出错');
  72. }
  73. }
  74. };
  75. xhr.send();
  76. };
  77. // 监听上传完成
  78. var Key;
  79. var submitTarget = document.getElementById('submitTarget');
  80. var showMessage = function (err, data) {
  81. console.log(err || data);
  82. document.getElementById('msg').innerText = err ? err : ('上传成功,ETag=' + data.ETag);
  83. };
  84. submitTarget.onload = function () {
  85. var search;
  86. try {
  87. search = submitTarget.contentWindow.location.search.substr(1);
  88. } catch (e) {
  89. showMessage('文件 ' + Key + ' 上传失败');
  90. }
  91. if (search) {
  92. var items = search.split('&');
  93. var i, arr, data = {};
  94. for (i = 0; i < items.length; i++) {
  95. arr = items[i].split('=');
  96. data[arr[0]] = decodeURIComponent(arr[1] || '');
  97. }
  98. showMessage(null, {url: prefix + camSafeUrlEncode(Key).replace(/%2F/g, '/'), ETag: data.etag});
  99. } else {
  100. }
  101. };
  102. // 发起上传
  103. document.getElementById('submitBtn').onclick = function (e) {
  104. var filePath = document.getElementById('fileSelector').value;
  105. if (!filePath) {
  106. document.getElementById('msg').innerText = '未选择上传文件';
  107. return;
  108. }
  109. Key = 'dir/' + filePath.match(/[\\\/]?([^\\\/]+)$/)[1]; // 这里指定上传目录和文件名
  110. getAuthorization({Method: 'POST', Pathname: '/'}, function (err, AuthData) {
  111. // 在当前目录下放一个空的 empty.html 以便让接口上传完成跳转回来
  112. document.getElementById('success_action_redirect').value = location.href.substr(0, location.href.lastIndexOf('/') + 1) + 'empty.html';
  113. document.getElementById('key').value = Key;
  114. document.getElementById('signature').value = AuthData.Authorization;
  115. document.getElementById('x-cos-security-token').value = AuthData.XCosSecurityToken || '';
  116. form.submit();
  117. });
  118. };
  119. })();
  120. </script>
  121. </body>
  122. </html>