Skip to content

Commit

Permalink
优化demo
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonxu committed Aug 29, 2017
1 parent 734a212 commit 2a5e27d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 18 deletions.
78 changes: 74 additions & 4 deletions demo/demo.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
var util = {
createFile: function (options) {
var buffer = new ArrayBuffer(options.size || 0);
var arr = new Uint8Array(buffer);
arr.forEach(function (char, i) {
arr[i] = 0;
});
var opt = {};
options.type && (opt.type = options.type);
var blob = new Blob([buffer], options);
return blob;
}
};

var getAuthorization = function (options, callback) {

// 方法一(推荐)
Expand Down Expand Up @@ -31,7 +45,6 @@ var cos = new COS({
ChunkParallelLimit: 3, // 控制单个文件下分片上传并发数
ChunkSize: 1024 * 1024, // 控制分片大小,单位 B
ProgressInterval: 1000, // 控制 onProgress 回调的间隔
Domain: '{{Bucket}}-{{AppId}}.{{Region}}.myqcloud.com', // 自定义域名
});
var TaskId;

Expand Down Expand Up @@ -96,7 +109,7 @@ function putBucket() {
function getBucket() {
cos.getBucket({
Bucket: config.Bucket,
Region: config.Region
Region: config.Region,
}, function (err, data) {
console.log(err || data);
});
Expand Down Expand Up @@ -154,7 +167,18 @@ function putBucketCors() {
"CORSRules": [{
"AllowedOrigin": ["*"],
"AllowedMethod": ["GET", "POST", "PUT", "DELETE", "HEAD"],
"AllowedHeader": ["origin", "accept", "content-type", "authorization"],
"AllowedHeader": [
"origin",
"accept",
"content-type",
"authorization",
"content-md5",
"x-cos-copy-source",
"x-cos-acl",
"x-cos-grant-read",
"x-cos-grant-write",
"x-cos-grant-full-control",
],
"ExposeHeader": ["ETag"],
"MaxAgeSeconds": "600"
}]
Expand Down Expand Up @@ -479,6 +503,51 @@ function sliceUploadFile() {
});
}

function selectFileToUpload() {
var input = document.createElement('input');
input.type = 'file';
input.onchange = function (e) {
var file = this.files[0]
if (file) {
if (file.size > 1024 * 1024) {
cos.sliceUploadFile({
Bucket: config.Bucket,
Region: config.Region,
Key: file.name,
Body: file,
TaskReady: function (tid) {
TaskId = tid;
},
onHashProgress: function (progressData) {
console.log('onHashProgress', JSON.stringify(progressData));
},
onProgress: function (progressData) {
console.log('onProgress', JSON.stringify(progressData));
},
}, function (err, data) {
console.log(err || data);
});
} else {
cos.putObject({
Bucket: config.Bucket,
Region: config.Region,
Key: file.name,
Body: file,
TaskReady: function (tid) {
TaskId = tid;
},
onProgress: function (progressData) {
console.log(JSON.stringify(progressData));
},
}, function (err, data) {
console.log(err || data);
});
}
}
};
input.click();
}

function cancelTask() {
cos.cancelTask(TaskId);
console.log('canceled');
Expand Down Expand Up @@ -540,7 +609,7 @@ function restartTask() {
'getBucketAcl',
'putBucketCors',
'getBucketCors',
'deleteBucketCors',
// 'deleteBucketCors', // 不提供
'putBucketTagging',
'getBucketTagging',
'deleteBucketTagging',
Expand All @@ -561,6 +630,7 @@ function restartTask() {
'deleteMultipleObject',
'abortUploadTask',
'sliceUploadFile',
'selectFileToUpload',
'cancelTask',
'pauseTask',
'restartTask',
Expand Down
1 change: 0 additions & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ <h1>cos-js-sdk-v5</h1>
</div>

<script src="../dist/cos-js-sdk-v5.js"></script>
<script src="./util.js"></script>
<script src="./config.js"></script>
<script src="./demo.js"></script>

Expand Down
13 changes: 0 additions & 13 deletions demo/util.js

This file was deleted.

0 comments on commit 2a5e27d

Please sign in to comment.