diff --git a/demo/js/qiniu.js b/demo/js/qiniu.js index f84dc38b..70142b13 100644 --- a/demo/js/qiniu.js +++ b/demo/js/qiniu.js @@ -249,7 +249,7 @@ function QiniuJsSDK() { } - var token = ''; + that.token = ''; var ctx = ''; plupload.extend(option, op, { @@ -260,17 +260,22 @@ function QiniuJsSDK() { }); var uploader = new plupload.Uploader(option); + var getUpToken = function() { - var ajax = that.createAjax(); - ajax.open('GET', uptoken_url, true); - ajax.setRequestHeader("If-Modified-Since", "0"); - ajax.onreadystatechange = function() { - if (ajax.readyState === 4 && ajax.status === 200) { - var res = that.parseJSON(ajax.responseText); - token = res.uptoken; - } - }; - ajax.send(); + if (!op.uptoken) { + var ajax = that.createAjax(); + ajax.open('GET', uptoken_url, true); + ajax.setRequestHeader("If-Modified-Since", "0"); + ajax.onreadystatechange = function() { + if (ajax.readyState === 4 && ajax.status === 200) { + var res = that.parseJSON(ajax.responseText); + that.token = res.uptoken; + } + }; + ajax.send(); + } else { + that.token = op.uptoken; + } }; uploader.bind('Init', function(up, params) { @@ -297,7 +302,7 @@ function QiniuJsSDK() { 'multipart': true, 'chunk_size': undefined, 'multipart_params': { - 'token': token, + 'token': that.token, 'key': file.name } }); @@ -315,7 +320,7 @@ function QiniuJsSDK() { 'multipart': false, 'chunk_size': chunk_size, 'headers': { - 'Authorization': 'UpToken ' + token + 'Authorization': 'UpToken ' + that.token }, 'multipart_params': {} }); @@ -426,7 +431,7 @@ function QiniuJsSDK() { var ajax = that.createAjax(); ajax.open('POST', url, true); ajax.setRequestHeader('Content-Type', 'text/plain;charset=UTF-8'); - ajax.setRequestHeader('Authorization', 'UpToken ' + token); + ajax.setRequestHeader('Authorization', 'UpToken ' + that.token); ajax.send(ctx); ajax.onreadystatechange = function() { if (ajax.readyState === 4) { diff --git a/src/qiniu.js b/src/qiniu.js index b41ec769..70142b13 100644 --- a/src/qiniu.js +++ b/src/qiniu.js @@ -249,7 +249,7 @@ function QiniuJsSDK() { } - var token = ''; + that.token = ''; var ctx = ''; plupload.extend(option, op, { @@ -260,17 +260,22 @@ function QiniuJsSDK() { }); var uploader = new plupload.Uploader(option); + var getUpToken = function() { - var ajax = that.createAjax(); - ajax.open('GET', uptoken_url, true); - ajax.setRequestHeader("If-Modified-Since", "0"); - ajax.onreadystatechange = function() { - if (ajax.readyState === 4 && ajax.status === 200) { - var res = that.parseJSON(ajax.responseText); - token = res.uptoken; - } - }; - ajax.send(); + if (!op.uptoken) { + var ajax = that.createAjax(); + ajax.open('GET', uptoken_url, true); + ajax.setRequestHeader("If-Modified-Since", "0"); + ajax.onreadystatechange = function() { + if (ajax.readyState === 4 && ajax.status === 200) { + var res = that.parseJSON(ajax.responseText); + that.token = res.uptoken; + } + }; + ajax.send(); + } else { + that.token = op.uptoken; + } }; uploader.bind('Init', function(up, params) { @@ -297,7 +302,7 @@ function QiniuJsSDK() { 'multipart': true, 'chunk_size': undefined, 'multipart_params': { - 'token': token, + 'token': that.token, 'key': file.name } }); @@ -315,7 +320,7 @@ function QiniuJsSDK() { 'multipart': false, 'chunk_size': chunk_size, 'headers': { - 'Authorization': 'UpToken ' + token + 'Authorization': 'UpToken ' + that.token }, 'multipart_params': {} }); @@ -355,6 +360,8 @@ function QiniuJsSDK() { errTip = '文件验证失败。请稍后重试。'; break; case plupload.HTTP_ERROR: + var errorObj = that.parseJSON(err.response); + var errorText = errorObj.error; switch (err.status) { case 400: errTip = "请求报文格式错误。"; @@ -373,6 +380,8 @@ function QiniuJsSDK() { break; case 614: errTip = "文件已存在。"; + errorObj = that.parseJSON(errorObj.error); + errorText = errorObj.error || 'file exists'; break; case 631: errTip = "指定空间不存在。"; @@ -384,8 +393,7 @@ function QiniuJsSDK() { errTip = "未知错误。"; break; } - var errorObj = that.parseJSON(err.response); - errTip = errTip + '(' + err.status + ':' + errorObj.error + ')'; + errTip = errTip + '(' + err.status + ':' + errorText + ')'; break; case plupload.SECURITY_ERROR: errTip = '安全配置错误。请联系网站管理员。'; @@ -407,6 +415,8 @@ function QiniuJsSDK() { if (Error_Handler) { Error_Handler(up, err, errTip); } + } else { + } up.refresh(); // Reposition Flash/Silverlight }; @@ -421,13 +431,22 @@ function QiniuJsSDK() { var ajax = that.createAjax(); ajax.open('POST', url, true); ajax.setRequestHeader('Content-Type', 'text/plain;charset=UTF-8'); - ajax.setRequestHeader('Authorization', 'UpToken ' + token); + ajax.setRequestHeader('Authorization', 'UpToken ' + that.token); ajax.send(ctx); ajax.onreadystatechange = function() { - if (ajax.readyState === 4 && ajax.status === 200) { - var info = ajax.responseText; - if (FileUploaded_Handler) { - FileUploaded_Handler(up, file, info); + if (ajax.readyState === 4) { + if (ajax.status === 200) { + var info = ajax.responseText; + if (FileUploaded_Handler) { + FileUploaded_Handler(up, file, info); + } + } else { + uploader.trigger('Error', { + status: ajax.status, + response: ajax.responseText, + file: file, + code: -200 + }); } } };