Skip to content

Commit

Permalink
修改 demo
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonxu committed Dec 26, 2018
1 parent 170da58 commit 76c6317
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
12 changes: 11 additions & 1 deletion demo/sts-form.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ <h1>Form 表单简单上传(兼容 IE8)</h1>
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' +
Expand Down Expand Up @@ -93,7 +103,7 @@ <h1>Form 表单简单上传(兼容 IE8)</h1>
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 {
}
};
Expand Down
12 changes: 11 additions & 1 deletion demo/sts-post.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ <h1>Ajax Post 上传</h1>
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' +
Expand Down Expand Up @@ -85,7 +95,7 @@ <h1>Ajax Post 上传</h1>
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);
}
Expand Down
12 changes: 11 additions & 1 deletion demo/sts-put.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ <h1>Ajax Put 上传</h1>
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' +
Expand Down Expand Up @@ -76,7 +86,7 @@ <h1>Ajax Put 上传</h1>

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);
Expand Down

0 comments on commit 76c6317

Please sign in to comment.