diff --git a/README.md b/README.md index 30464d3d..8482b5c0 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,20 @@ qiniu-js-sdk dragdrop: true, //开启可拖曳上传 drop_element: 'container', //拖曳上传区域元素的ID,拖曳文件或文件夹后可触发上传 chunk_size: '4mb', //分块上传时,每片的体积 - auto_start: true, //选择文件后自动上传,若关闭需要自己绑定事件触发上传 + auto_start: true, //选择文件后自动上传,若关闭需要自己绑定事件触发上传, + //x_vals : { + // 自定义变量,参考http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html + // 'time' : function(up,file) { + // var time = (new Date()).getTime(); + // do something with 'time' + // returnt time; + // }, + // 'size' : function(up,file) { + // var size = file.size; + // do something with 'size' + // return size; + // } + //}, init: { 'FilesAdded': function(up, files) { plupload.each(files, function(file) { diff --git a/demo/js/qiniu.js b/demo/js/qiniu.js index 0b9fb4d8..36db0ae0 100644 --- a/demo/js/qiniu.js +++ b/demo/js/qiniu.js @@ -344,6 +344,16 @@ function QiniuJsSDK() { }; } + var x_vars = op.x_vars; + if (x_vars !== undefined && typeof x_vars === 'object') { + for (var x_key in x_vars) { + if (x_vars.hasOwnProperty(x_key) && typeof x_vars[x_key] === 'function') { + multipart_params_obj['x:' + x_key] = x_vars[x_key](up, file); + } + } + } + + up.setOption({ 'url': 'http://up.qiniu.com/', 'multipart': true, @@ -480,13 +490,23 @@ function QiniuJsSDK() { ctx = ctx ? ctx : res.ctx; if (ctx) { var key = ''; - if (!op.save_key) { key = getFileKey(up, file, that.key_handler); key = key ? '/key/' + that.URLSafeBase64Encode(key) : ''; } - var url = 'http://up.qiniu.com/mkfile/' + file.size + key; + var x_vars = op.x_vars, + x_vars_url = ''; + if (x_vars !== undefined && typeof x_vars === 'object') { + for (var x_key in x_vars) { + if (x_vars.hasOwnProperty(x_key) && typeof x_vars[x_key] === 'function') { + var x_val = that.URLSafeBase64Encode(x_vars[x_key](up, file)); + x_vars_url += '/x:' + x_key + '/' + x_val; + } + } + } + + var url = 'http://up.qiniu.com/mkfile/' + file.size + key + x_vars_url; var ajax = that.createAjax(); ajax.open('POST', url, true); ajax.setRequestHeader('Content-Type', 'text/plain;charset=UTF-8'); diff --git a/src/qiniu.js b/src/qiniu.js index 0b9fb4d8..36db0ae0 100644 --- a/src/qiniu.js +++ b/src/qiniu.js @@ -344,6 +344,16 @@ function QiniuJsSDK() { }; } + var x_vars = op.x_vars; + if (x_vars !== undefined && typeof x_vars === 'object') { + for (var x_key in x_vars) { + if (x_vars.hasOwnProperty(x_key) && typeof x_vars[x_key] === 'function') { + multipart_params_obj['x:' + x_key] = x_vars[x_key](up, file); + } + } + } + + up.setOption({ 'url': 'http://up.qiniu.com/', 'multipart': true, @@ -480,13 +490,23 @@ function QiniuJsSDK() { ctx = ctx ? ctx : res.ctx; if (ctx) { var key = ''; - if (!op.save_key) { key = getFileKey(up, file, that.key_handler); key = key ? '/key/' + that.URLSafeBase64Encode(key) : ''; } - var url = 'http://up.qiniu.com/mkfile/' + file.size + key; + var x_vars = op.x_vars, + x_vars_url = ''; + if (x_vars !== undefined && typeof x_vars === 'object') { + for (var x_key in x_vars) { + if (x_vars.hasOwnProperty(x_key) && typeof x_vars[x_key] === 'function') { + var x_val = that.URLSafeBase64Encode(x_vars[x_key](up, file)); + x_vars_url += '/x:' + x_key + '/' + x_val; + } + } + } + + var url = 'http://up.qiniu.com/mkfile/' + file.size + key + x_vars_url; var ajax = that.createAjax(); ajax.open('POST', url, true); ajax.setRequestHeader('Content-Type', 'text/plain;charset=UTF-8');