Skip to content

Commit 54db574

Browse files
duan007abinghaiwang
authored andcommitted
feat: add sample about cname's usage (#453)
feat: add sample about cname's usage
1 parent 8714425 commit 54db574

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

example/index.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ <h1>OSS <small>in</small> Browser</h1>
6262
<div class="form-group">
6363
<label>Store as</label>
6464
<input type="text" class="form-control" id="object-key-content" value="object" />
65+
</div>
6566
<div class="form-group">
6667
<input type="button" class="btn btn-primary" id="content-button" value="Save" />
6768
</div>
@@ -183,6 +184,25 @@ <h1>OSS <small>in</small> Browser</h1>
183184
</div>
184185
</div>
185186
</td>
187+
<td>
188+
<div class="panel panel-success">
189+
<div class="panel-heading">Custom Domain Usage</div>
190+
<div class="panel-body">
191+
<form action="" class="form-horizontal">
192+
<div class="form-group">
193+
<input type="text" class="form-control" id="key-cname" placeholder="Please Input Cname!" />
194+
<label>object name to download</label>
195+
<input type="text" class="form-control" id="key-cname-objectName" value="cnameObject" />
196+
<div>
197+
<input type="button" class="btn btn-primary" id="key-cname-button" value="Save" />
198+
</div>
199+
</div>
200+
</form>
201+
</div>
202+
</div>
203+
</td>
204+
</tr>
205+
<tr>
186206
<td colspan="2">
187207
Powered by
188208
<a href="https://www.aliyun.com/product/oss">OSS</a> &

example/index.js

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ const base64progress = function (p) {
108108
* @param filename set up a meaningful suffix, or you can set mime type in options
109109
* @returns {File|*}
110110
*/
111-
function dataURLtoFile(dataurl, filename) {
111+
const dataURLtoFile = function dataURLtoFile(dataurl, filename) {
112112
const arr = dataurl.split(',');
113113
const mime = arr[0].match(/:(.*?);/)[1];
114114
const bstr = atob(arr[1]);
@@ -118,10 +118,9 @@ function dataURLtoFile(dataurl, filename) {
118118
u8arr[n] = bstr.charCodeAt(n);
119119
}
120120
return new Blob([u8arr], { type: mime });// if env support File, also can use this: return new File([u8arr], filename, { type: mime });
121-
}
122-
121+
};
123122

124-
const uploadBase64Img = function (client) {
123+
const uploadBase64Img = function uploadBase64Img(client) {
125124
const base64Content = document.getElementById('base64-file-content').value.trim();
126125
const key = document.getElementById('base64-object-key-file').value.trim() || 'object';
127126
if (base64Content.indexOf('data:image') === 0) {
@@ -226,6 +225,38 @@ const downloadFile = function (client) {
226225
return result;
227226
};
228227

228+
const cnameUsage = function (cname) {
229+
if (!cname) {
230+
alert('请输入cname!');
231+
return;
232+
}
233+
const url = appServer;
234+
$.ajax({
235+
url,
236+
}).then((result) => {
237+
const creds = result;
238+
const client = new OSS.Wrapper({
239+
accessKeyId: creds.AccessKeyId,
240+
accessKeySecret: creds.AccessKeySecret,
241+
stsToken: creds.SecurityToken,
242+
endpoint: cname,
243+
cname: true,
244+
region,
245+
bucket,
246+
});
247+
248+
const filename = document.getElementById('key-cname-objectName').value.trim();
249+
console.log(filename);
250+
251+
const res = client.signatureUrl(filename, {
252+
response: {
253+
'content-disposition': `attachment; filename="${filename}"`,
254+
},
255+
});
256+
window.location = res;
257+
});
258+
};
259+
229260
window.onload = function () {
230261
document.getElementById('file-button').onclick = function () {
231262
if (uploadFileClient) {
@@ -265,5 +296,10 @@ window.onload = function () {
265296
applyTokenDo(uploadBase64Img);
266297
};
267298

299+
document.getElementById('key-cname-button').onclick = function () {
300+
const cname = document.getElementById('key-cname').value;
301+
cnameUsage(cname);
302+
};
303+
268304
applyTokenDo(listFiles);
269305
};

0 commit comments

Comments
 (0)