Skip to content

Commit dc2b7f0

Browse files
committed
Merge pull request #13 from qiniupd/8351_add_x_val_feature
add x:val feature in js-sdk
2 parents 49ebd2e + 4b17f71 commit dc2b7f0

File tree

3 files changed

+58
-5
lines changed

3 files changed

+58
-5
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,20 @@ qiniu-js-sdk
7171
dragdrop: true, //开启可拖曳上传
7272
drop_element: 'container', //拖曳上传区域元素的ID,拖曳文件或文件夹后可触发上传
7373
chunk_size: '4mb', //分块上传时,每片的体积
74-
auto_start: true, //选择文件后自动上传,若关闭需要自己绑定事件触发上传
74+
auto_start: true, //选择文件后自动上传,若关闭需要自己绑定事件触发上传,
75+
//x_vals : {
76+
// 自定义变量,参考http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html
77+
// 'time' : function(up,file) {
78+
// var time = (new Date()).getTime();
79+
// do something with 'time'
80+
// returnt time;
81+
// },
82+
// 'size' : function(up,file) {
83+
// var size = file.size;
84+
// do something with 'size'
85+
// return size;
86+
// }
87+
//},
7588
init: {
7689
'FilesAdded': function(up, files) {
7790
plupload.each(files, function(file) {

demo/js/qiniu.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,16 @@ function QiniuJsSDK() {
344344
};
345345
}
346346

347+
var x_vars = op.x_vars;
348+
if (x_vars !== undefined && typeof x_vars === 'object') {
349+
for (var x_key in x_vars) {
350+
if (x_vars.hasOwnProperty(x_key) && typeof x_vars[x_key] === 'function') {
351+
multipart_params_obj['x:' + x_key] = x_vars[x_key](up, file);
352+
}
353+
}
354+
}
355+
356+
347357
up.setOption({
348358
'url': 'http://up.qiniu.com/',
349359
'multipart': true,
@@ -480,13 +490,23 @@ function QiniuJsSDK() {
480490
ctx = ctx ? ctx : res.ctx;
481491
if (ctx) {
482492
var key = '';
483-
484493
if (!op.save_key) {
485494
key = getFileKey(up, file, that.key_handler);
486495
key = key ? '/key/' + that.URLSafeBase64Encode(key) : '';
487496
}
488497

489-
var url = 'http://up.qiniu.com/mkfile/' + file.size + key;
498+
var x_vars = op.x_vars,
499+
x_vars_url = '';
500+
if (x_vars !== undefined && typeof x_vars === 'object') {
501+
for (var x_key in x_vars) {
502+
if (x_vars.hasOwnProperty(x_key) && typeof x_vars[x_key] === 'function') {
503+
var x_val = that.URLSafeBase64Encode(x_vars[x_key](up, file));
504+
x_vars_url += '/x:' + x_key + '/' + x_val;
505+
}
506+
}
507+
}
508+
509+
var url = 'http://up.qiniu.com/mkfile/' + file.size + key + x_vars_url;
490510
var ajax = that.createAjax();
491511
ajax.open('POST', url, true);
492512
ajax.setRequestHeader('Content-Type', 'text/plain;charset=UTF-8');

src/qiniu.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,16 @@ function QiniuJsSDK() {
344344
};
345345
}
346346

347+
var x_vars = op.x_vars;
348+
if (x_vars !== undefined && typeof x_vars === 'object') {
349+
for (var x_key in x_vars) {
350+
if (x_vars.hasOwnProperty(x_key) && typeof x_vars[x_key] === 'function') {
351+
multipart_params_obj['x:' + x_key] = x_vars[x_key](up, file);
352+
}
353+
}
354+
}
355+
356+
347357
up.setOption({
348358
'url': 'http://up.qiniu.com/',
349359
'multipart': true,
@@ -480,13 +490,23 @@ function QiniuJsSDK() {
480490
ctx = ctx ? ctx : res.ctx;
481491
if (ctx) {
482492
var key = '';
483-
484493
if (!op.save_key) {
485494
key = getFileKey(up, file, that.key_handler);
486495
key = key ? '/key/' + that.URLSafeBase64Encode(key) : '';
487496
}
488497

489-
var url = 'http://up.qiniu.com/mkfile/' + file.size + key;
498+
var x_vars = op.x_vars,
499+
x_vars_url = '';
500+
if (x_vars !== undefined && typeof x_vars === 'object') {
501+
for (var x_key in x_vars) {
502+
if (x_vars.hasOwnProperty(x_key) && typeof x_vars[x_key] === 'function') {
503+
var x_val = that.URLSafeBase64Encode(x_vars[x_key](up, file));
504+
x_vars_url += '/x:' + x_key + '/' + x_val;
505+
}
506+
}
507+
}
508+
509+
var url = 'http://up.qiniu.com/mkfile/' + file.size + key + x_vars_url;
490510
var ajax = that.createAjax();
491511
ajax.open('POST', url, true);
492512
ajax.setRequestHeader('Content-Type', 'text/plain;charset=UTF-8');

0 commit comments

Comments
 (0)