-
Notifications
You must be signed in to change notification settings - Fork 567
/
Copy pathpolicy-form.html
144 lines (129 loc) · 5.57 KB
/
policy-form.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Form 表单简单上传</title>
<style>h1, h2 {font-weight: normal;}#msg {margin-top:10px;}</style>
</head>
<body>
<h1>PostObject 上传(Policy 保护,Form 表单上传)</h1>
<div>最低兼容到 IE6 上传,使用 policy 签名保护,不支持 onprogress</div>
<form id="form" target="submitTarget" action="" method="post" enctype="multipart/form-data" accept="*/*">
<!-- file 字段放在表单最后,避免文件内容过长影响签名判断和鉴权 -->
<input id="fileSelector" name="file" type="file">
<input id="submitBtn" type="button" value="提交">
</form>
<iframe id="submitTarget" name="submitTarget" style="display:none;" frameborder="0"></iframe>
<div id="msg"></div>
<script src="common/cos-auth.min.js"></script>
<script>
(function () {
// 请求用到的参数
var Bucket = 'test-1250000000';
var Region = 'ap-guangzhou';
var protocol = location.protocol === 'https:' ? 'https:' : 'http:';
var prefix = protocol + '//' + Bucket + '.cos.' + Region + '.myqcloud.com/';
var fileSelector = document.getElementById('fileSelector');
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 getPostPolicyCredentials = function (opt, callback) {
var url = 'http://127.0.0.1:3000/post-policy?key=' + encodeURIComponent(opt.Key);
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onreadystatechange = function (e) {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
var credentials;
try {
credentials = (new Function('return ' + xhr.responseText))();
} catch (e) {}
if (credentials) {
callback(null, credentials);
} else {
console.error(xhr.responseText);
callback('获取签名出错');
}
} else {
callback('获取签名出错');
}
}
};
xhr.send();
};
// 监听上传完成
var Key;
var submitTarget = document.getElementById('submitTarget');
var showMessage = function (err, data) {
console.log(err || data);
document.getElementById('msg').innerText = err ? err : ('上传成功,ETag=' + data.ETag);
};
submitTarget.onload = function () {
var search;
try {
search = submitTarget.contentWindow.location.search.substr(1);
} catch (e) {
showMessage('文件 ' + Key + ' 上传失败');
}
if (search) {
var items = search.split('&');
var i, arr, data = {};
for (i = 0; i < items.length; i++) {
arr = items[i].split('=');
data[arr[0]] = decodeURIComponent(arr[1] || '');
}
showMessage(null, {url: prefix + camSafeUrlEncode(Key).replace(/%2F/g, '/'), ETag: data.etag});
} else {
}
};
var setFormField = function (key, value) {
var el = document.getElementById(key);
if (!el) {
el = document.createElement('input');
el.hidden = true;
el.id = key;
el.name = key;
form.insertBefore(el, fileSelector);
}
el.setAttribute('value', value); // 需要保证 file 在表单最后
el.value = value;
};
// 发起上传
document.getElementById('submitBtn').onclick = function (e) {
var filePath = document.getElementById('fileSelector').value;
if (!filePath) {
document.getElementById('msg').innerText = '未选择上传文件';
return;
}
Key = 'dir/' + filePath.match(/[\\\/]?([^\\\/]+)$/)[1]; // 这里指定上传目录和文件名
// 获取签名保护字段
getPostPolicyCredentials({
Key: Key,
}, function (err, credentials) {
// 在当前目录下放一个空的 empty.html 以便让接口上传完成跳转回来
setFormField('success_action_redirect', location.href.substr(0, location.href.lastIndexOf('/') + 1) + 'empty.html');
setFormField('key', Key);
// 使用 policy 签名保护格式
credentials.securityToken && setFormField('x-cos-security-token', credentials.securityToken);
setFormField('q-sign-algorithm', credentials.qSignAlgorithm);
setFormField('q-ak', credentials.qAk);
setFormField('q-key-time', credentials.qKeyTime);
setFormField('q-signature', credentials.qSignature);
setFormField('policy', credentials.policy);
// 提交表单
form.submit();
});
};
})();
</script>
</body>
</html>