Created
June 2, 2015 14:58
-
-
Save leeight/a7ba802b9774ac7f0c0c to your computer and use it in GitHub Desktop.
BOS WEB SDK
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[toc] | |
# BOS WEB SDK | |
## 目的 | |
为 BOS 的用户提供一种更简便,直接的方式管理 BOS 上面所属的资源。 | |
BOS WEB SDK 提供两种服务?工具?: | |
1. BOS Web SDK | |
2. BOS Web Uploader | |
BOS Web Uploader是基于BOS Web SDK开发的一个上传控件,可以直接嵌入在自己的站点里面,便捷的完成文件的上传工作。 | |
## 用法 | |
在页面中通过 esl 来加载`baidubce.sdk` | |
```html | |
<html> | |
<head> | |
<script src="esl.js"></script> | |
<script> | |
require.config({ | |
paths: { | |
"baidubce.sdk": "//bos.bj.baidubce.com/public/baidubce.sdk.js" | |
} | |
}); | |
</script> | |
</head> | |
<body> | |
<button id="btn">Upload Image</button> | |
</body> | |
</html> | |
``` | |
然后调用所需要的API | |
```js | |
define(function (require) { | |
var sdk = require('baidubce.sdk'); | |
var uploader; | |
document.getElementId('btn').onclick = function () { | |
uploader = new sdk.BosUploader({ | |
bucket: 'my-bucket', | |
authServerUrl: 'http://sigunature.server.com/ack', | |
width: 500, | |
height: 300 | |
}); | |
uploader.open(); | |
}; | |
}); | |
``` | |
## API Overview | |
### 构造函数 | |
#### sdk.BosUploader | |
创建一个 `uploader` 实例,支持的参数如下: | |
|*name*|*type*|*description*|*optional*| | |
|------|------|-------------|----------| | |
|bucket|string|需要上传的bucket名字|必选| | |
|authServerUrl|string|用来计算签名的服务器地址(注意做一些必要的安全性验证)|必选| | |
|width|number|uploader的宽度|可选| | |
|height|number|uploader的高度|可选| | |
### 方法 | |
#### open | |
打开`uploader`浮层,浮层里面嵌入的是一个 `bos.bj.baidubce.com`的页面,这个页面来完成跟 BOS API 的交互工作,并且把上传的进度以事件的形式通知给宿主页面。 | |
#### close | |
关闭`uploader`浮层 | |
### 事件 | |
#### bosprogress | |
```js | |
uploader.on('bosprogress', function (evt) { | |
console.log(evt.loaded / evt.total); | |
}); | |
``` | |
#### finish | |
所有内容上传完毕之后,派发这个事件. | |
```js | |
uploader.on('finish', function () { | |
uploader.close(); | |
}); | |
``` | |
#### error | |
上传过程中出现了任何错误,派发这个事件。 | |
```js | |
uploader.on('error', function (evt) { | |
console.log(evt); | |
}); | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment