From 76c6317a26256a7b18697050256dfc7142f26997 Mon Sep 17 00:00:00 2001 From: carsonxu <459452372@qq.com> Date: Wed, 26 Dec 2018 18:59:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20demo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo/sts-form.html | 12 +++++++++++- demo/sts-post.html | 12 +++++++++++- demo/sts-put.html | 12 +++++++++++- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/demo/sts-form.html b/demo/sts-form.html index e036225..6ea8bb9 100644 --- a/demo/sts-form.html +++ b/demo/sts-form.html @@ -37,6 +37,16 @@

Form 表单简单上传(兼容 IE8)

var form = document.getElementById('form'); form.action = prefix; + // 对更多字符编码的 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) { // var url = 'http://127.0.0.1:3000/sts' + @@ -93,7 +103,7 @@

Form 表单简单上传(兼容 IE8)

arr = items[i].split('='); data[arr[0]] = decodeURIComponent(arr[1] || ''); } - showMessage(null, {url: prefix + encodeURIComponent(Key).replace(/%2F/g, '/'), ETag: data.etag}); + showMessage(null, {url: prefix + camSafeUrlEncode(Key).replace(/%2F/g, '/'), ETag: data.etag}); } else { } }; diff --git a/demo/sts-post.html b/demo/sts-post.html index a9bb0ad..89fd2c9 100644 --- a/demo/sts-post.html +++ b/demo/sts-post.html @@ -31,6 +31,16 @@

Ajax Post 上传

var protocol = location.protocol === 'https:' ? 'https:' : 'http:'; var prefix = protocol + '//' + Bucket + '.cos.' + Region + '.myqcloud.com/'; + // 对更多字符编码的 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) { // var url = 'http://127.0.0.1:3000/sts' + @@ -85,7 +95,7 @@

Ajax Post 上传

xhr.onload = function () { if (Math.floor(xhr.status / 100) === 2) { var ETag = xhr.getResponseHeader('etag'); - callback(null, {url: prefix + encodeURIComponent(Key).replace(/%2F/g, '/'), ETag: ETag}); + callback(null, {url: prefix + camSafeUrlEncode(Key).replace(/%2F/g, '/'), ETag: ETag}); } else { callback('文件 ' + Key + ' 上传失败,状态码:' + xhr.status); } diff --git a/demo/sts-put.html b/demo/sts-put.html index a49351a..081e399 100644 --- a/demo/sts-put.html +++ b/demo/sts-put.html @@ -32,6 +32,16 @@

Ajax Put 上传

var protocol = location.protocol === 'https:' ? 'https:' : 'http:'; var prefix = protocol + '//' + Bucket + '.cos.' + Region + '.myqcloud.com/'; + // 对更多字符编码的 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) { // var url = 'http://127.0.0.1:3000/sts-auth' + @@ -76,7 +86,7 @@

Ajax Put 上传

var auth = info.Authorization; var XCosSecurityToken = info.XCosSecurityToken; - var url = prefix + Key; + var url = prefix + camSafeUrlEncode(Key).replace(/%2F/, '/'); var xhr = new XMLHttpRequest(); xhr.open('PUT', url, true); xhr.setRequestHeader('Authorization', auth);