From af65201ebe08278690d223f3e6f9251eb26b5052 Mon Sep 17 00:00:00 2001 From: meilixie Date: Thu, 13 Aug 2015 18:51:32 +0800 Subject: [PATCH 1/5] add upload cancel function but if I cancel one upload file then all files will be cancel --- demo/js/main.js | 15 ++++++++++----- demo/js/qiniu.js | 1 + demo/js/ui.js | 32 +++++++++++++++++++++++--------- demo/main.css | 10 ++++++++++ src/qiniu.js | 1 + src/qiniu.min.js | 2 +- 6 files changed, 46 insertions(+), 15 deletions(-) diff --git a/demo/js/main.js b/demo/js/main.js index d1dd3aa8..c0ca6241 100644 --- a/demo/js/main.js +++ b/demo/js/main.js @@ -35,6 +35,7 @@ $(function() { plupload.each(files, function(file) { var progress = new FileProgress(file, 'fsUploadProgress'); progress.setStatus("等待..."); + progress.uploadCancel(up); }); }, 'BeforeUpload': function(up, file) { @@ -43,12 +44,16 @@ $(function() { if (up.runtime === 'html5' && chunk_size) { progress.setChunkProgess(chunk_size); } + progress.uploadCancel(up); }, 'UploadProgress': function(up, file) { var progress = new FileProgress(file, 'fsUploadProgress'); + var chunk_size = plupload.parseSize(this.getOption('chunk_size')); progress.setProgress(file.percent + "%", file.speed, chunk_size); + progress.uploadCancel(up); + }, 'UploadComplete': function() { $('#success').show(); @@ -58,11 +63,11 @@ $(function() { progress.setComplete(up, info); }, 'Error': function(up, err, errTip) { - $('table').show(); - var progress = new FileProgress(err.file, 'fsUploadProgress'); - progress.setError(); - progress.setStatus(errTip); - } + $('table').show(); + var progress = new FileProgress(err.file, 'fsUploadProgress'); + progress.setError(); + progress.setStatus(errTip); + } // , // 'Key': function(up, file) { // var key = ""; diff --git a/demo/js/qiniu.js b/demo/js/qiniu.js index 03500379..bcbe9ff2 100644 --- a/demo/js/qiniu.js +++ b/demo/js/qiniu.js @@ -440,6 +440,7 @@ function QiniuJsSDK() { }); uploader.bind('UploadProgress', function(up, file) { + // 计算速度 speedCalInfo.currentTime = new Date().getTime(); diff --git a/demo/js/ui.js b/demo/js/ui.js index e289dddf..733c8cdc 100644 --- a/demo/js/ui.js +++ b/demo/js/ui.js @@ -32,7 +32,6 @@ function FileProgress(file, targetID) { var progressBarWrapper = $("
"); progressBarWrapper.addClass("progress progress-striped"); - var progressBar = $("
"); progressBar.addClass("progress-bar progress-bar-info") .attr('role', 'progressbar') @@ -44,22 +43,18 @@ function FileProgress(file, targetID) { var progressBarPercent = $(''); progressBarPercent.text(fileSize); - - var progressCancel = $(''); - progressCancel.hide().addClass('progressCancel').text(''); - + var progressCancel = $(''); + progressCancel.show().addClass('progressCancel').text('×'); progressBar.append(progressBarPercent); progressBarWrapper.append(progressBar); progressBarBox.append(progressBarWrapper); progressBarBox.append(progressCancel); - var progressBarStatus = $('
'); progressBarBox.append(progressBarStatus); progressBarTd.append(progressBarBox); - Wrappeer.append(progressText); Wrappeer.append(progressSize); Wrappeer.append(progressBarTd); @@ -141,6 +136,7 @@ FileProgress.prototype.setProgress = function(percentage, speed, chunk_size) { if (file.status !== plupload.DONE && percentage === 100) { percentage = 99; } + progressbar.attr('aria-valuenow', percentage).css('width', percentage + '%'); if (chunk_size) { @@ -187,7 +183,8 @@ FileProgress.prototype.setProgress = function(percentage, speed, chunk_size) { }; FileProgress.prototype.setComplete = function(up, info) { - var td = this.fileProgressWrapper.find('td:eq(2) .progress'); + var td = this.fileProgressWrapper.find('td:eq(2)'), + tdProgress = td.find('.progress'); var res = $.parseJSON(info); var url; @@ -203,7 +200,8 @@ FileProgress.prototype.setComplete = function(up, info) { "
Hash:" + res.hash + "
"; } - td.html(str).removeClass().next().next('.status').hide(); + tdProgress.html(str).removeClass().next().next('.status').hide(); + td.find('.progressCancel').hide(); var progressNameTd = this.fileProgressWrapper.find('.progressName'); var imageView = '?imageView2/1/w/100/h/100'; @@ -361,6 +359,7 @@ FileProgress.prototype.setCancelled = function(manual) { } this.fileProgressWrapper.attr('class', progressContainer); this.fileProgressWrapper.find('td .progress .progress-bar-info').css('width', 0); + this.fileProgressWrapper.find('td:eq(2) .btn-default').hide(); }; FileProgress.prototype.setStatus = function(status, isUploading) { @@ -369,6 +368,21 @@ FileProgress.prototype.setStatus = function(status, isUploading) { } }; +FileProgress.prototype.uploadCancel = function(up) { + var self = this; + if (up) { + + self.fileProgressWrapper.find('td:eq(2) .progressCancel').on('click', function(){ + self.setCancelled(false); + self.setStatus("取消上传"); + // var status_before = self.file.status; + up.removeFile(self.file); + up.stop(); + return true; + }); + } + +}; FileProgress.prototype.appear = function() { if (this.getTimer() !== null) { diff --git a/demo/main.css b/demo/main.css index 91e1e91b..98e36abf 100644 --- a/demo/main.css +++ b/demo/main.css @@ -50,6 +50,10 @@ h1 .view_code { position: relative; height: 100%; } +tr .progress{ + width: 96%; + display: inline-block; +} tr .progress-bar { min-height: 20px; float: none; @@ -63,6 +67,12 @@ tr .progress .chunk-status { left: 24px; top: 0; } +tr .progressCancel{ + display: inline-block; + float: right; + position: relative; + z-index: 100; +} .status { position: absolute; width: 100%; diff --git a/src/qiniu.js b/src/qiniu.js index 03500379..bcbe9ff2 100644 --- a/src/qiniu.js +++ b/src/qiniu.js @@ -440,6 +440,7 @@ function QiniuJsSDK() { }); uploader.bind('UploadProgress', function(up, file) { + // 计算速度 speedCalInfo.currentTime = new Date().getTime(); diff --git a/src/qiniu.min.js b/src/qiniu.min.js index 156e26cc..a94045ee 100644 --- a/src/qiniu.min.js +++ b/src/qiniu.min.js @@ -1 +1 @@ -function QiniuJsSDK(){this.detectIEVersion=function(){for(var a=4,b=document.createElement("div"),c=b.getElementsByTagName("i");b.innerHTML="",c[0];)a++;return a>4?a:!1},this.isImage=function(a){var b,c="",d=["png","jpg","jpeg","gif","bmp"],e=/\.([a-zA-Z0-9]+)(\?|\@|$)/;if(!a||!e.test(a))return!1;b=e.exec(a),c=b[1].toLowerCase();for(var f=0,g=d.length;g>f;f++)if(c===d[f])return!0;return!1},this.getFileExtension=function(a){var b,c=a.split(".");return b=1===c.length||""===c[0]&&2===c.length?"":c.pop().toLowerCase()},this.utf8_encode=function(a){if(null===a||"undefined"==typeof a)return"";var b,c,d=a+"",e="",f=0;b=c=0,f=d.length;for(var g=0;f>g;g++){var h=d.charCodeAt(g),i=null;if(128>h)c++;else if(h>127&&2048>h)i=String.fromCharCode(h>>6|192,63&h|128);else if(63488&h^!0)i=String.fromCharCode(h>>12|224,h>>6&63|128,63&h|128);else{if(64512&h^!0)throw new RangeError("Unmatched trail surrogate at "+g);var j=d.charCodeAt(++g);if(64512&j^!0)throw new RangeError("Unmatched lead surrogate at "+(g-1));h=((1023&h)<<10)+(1023&j)+65536,i=String.fromCharCode(h>>18|240,h>>12&63|128,h>>6&63|128,63&h|128)}null!==i&&(c>b&&(e+=d.slice(b,c)),e+=i,b=c=g+1)}return c>b&&(e+=d.slice(b,f)),e},this.base64_encode=function(a){var b,c,d,e,f,g,h,i,j="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",k=0,l=0,m="",n=[];if(!a)return a;a=this.utf8_encode(a+"");do b=a.charCodeAt(k++),c=a.charCodeAt(k++),d=a.charCodeAt(k++),i=b<<16|c<<8|d,e=i>>18&63,f=i>>12&63,g=i>>6&63,h=63&i,n[l++]=j.charAt(e)+j.charAt(f)+j.charAt(g)+j.charAt(h);while(k=f&&b.chunk_size&&b.runtimes.indexOf("flash")>=0?b.chunk_size=0:g?b.chunk_size=0:(c=20,d=4<d&&(b.chunk_size=d))};h();var i=function(){if(b.uptoken)a.token=b.uptoken;else{var c=a.createAjax();c.open("GET",a.uptoken_url,!0),c.setRequestHeader("If-Modified-Since","0"),c.onreadystatechange=function(){if(4===c.readyState&&200===c.status){var b=a.parseJSON(c.responseText);a.token=b.uptoken}},c.send()}},j=function(c,d,e){var f="",g=!1;if(!b.save_key)if(g=c.getOption&&c.getOption("unique_names"),g=g||c.settings&&c.settings.unique_names){var h=a.getFileExtension(d.name);f=h?d.id+"."+h:d.id}else f="function"==typeof e?e(c,d):d.name;return f};plupload.extend(c,b,{url:"http://upload.qiniu.com",multipart_params:{token:""}});var k=new plupload.Uploader(c);return k.bind("Init",function(){i()}),k.init(),k.bind("FilesAdded",function(a,b){var c=a.getOption&&a.getOption("auto_start");c=c||a.settings&&a.settings.auto_start,c&&plupload.each(b,function(){a.start()}),a.refresh()}),k.bind("BeforeUpload",function(c,d){d.speed=d.speed||0,f="";var e=function(c,d,e){g.startTime=(new Date).getTime();var f;f=b.save_key?{token:a.token}:{key:j(c,d,e),token:a.token};var h=b.x_vars;if(void 0!==h&&"object"==typeof h)for(var i in h)h.hasOwnProperty(i)&&("function"==typeof h[i]?f["x:"+i]=h[i](c,d):"object"!=typeof h[i]&&(f["x:"+i]=h[i]));c.setOption({url:"http://upload.qiniu.com/",multipart:!0,chunk_size:void 0,multipart_params:f})},h=c.getOption&&c.getOption("chunk_size");if(h=h||c.settings&&c.settings.chunk_size,"html5"===k.runtime&&h)if(d.sizem-n&&100!==i.percent&&d.size===i.total?(d.percent=i.percent,d.loaded=i.offset,f=i.ctx,g.isResumeUpload=!0,g.resumeFilesize=i.offset,i.offset+l>d.size&&(l=d.size-i.offset)):localStorage.removeItem(d.name)}g.startTime=(new Date).getTime(),c.setOption({url:"http://upload.qiniu.com/mkblk/"+l,multipart:!1,chunk_size:h,required_features:"chunks",headers:{Authorization:"UpToken "+a.token},multipart_params:{}})}else e(c,d,a.key_handler)}),k.bind("UploadProgress",function(a,b){g.currentTime=(new Date).getTime();var c=g.currentTime-g.startTime,d=b.loaded||0;g.isResumeUpload&&(d=b.loaded-g.resumeFilesize),b.speed=(d/c*1e3).toFixed(0)||0}),k.bind("ChunkUploaded",function(b,c,d){var e=a.parseJSON(d.response);f=f?f+","+e.ctx:e.ctx;var g=d.total-d.offset,h=b.getOption&&b.getOption("chunk_size");h=h||b.settings&&b.settings.chunk_size,h>g&&b.setOption({url:"http://upload.qiniu.com/mkblk/"+g}),localStorage.setItem(c.name,JSON.stringify({ctx:f,percent:c.percent,total:d.total,offset:d.offset,time:(new Date).getTime()}))}),k.bind("Error",function(b){return function(c,d){var e="",f=d.file;if(f){switch(d.code){case plupload.FAILED:e="上传失败。请稍后再试。";break;case plupload.FILE_SIZE_ERROR:var g=c.getOption&&c.getOption("max_file_size");g=g||c.settings&&c.settings.max_file_size,e="浏览器最大可上传"+g+"。更大文件请使用命令行工具。";break;case plupload.FILE_EXTENSION_ERROR:e="文件验证失败。请稍后重试。";break;case plupload.HTTP_ERROR:var h=a.parseJSON(d.response),i=h.error;switch(d.status){case 400:e="请求报文格式错误。";break;case 401:e="客户端认证授权失败。请重试或提交反馈。";break;case 405:e="客户端请求错误。请重试或提交反馈。";break;case 579:e="资源上传成功,但回调失败。";break;case 599:e="网络连接异常。请重试或提交反馈。";break;case 614:e="文件已存在。";try{h=a.parseJSON(h.error),i=h.error||"file exists"}catch(j){i=h.error||"file exists"}break;case 631:e="指定空间不存在。";break;case 701:e="上传数据块校验出错。请重试或提交反馈。";break;default:e="未知错误。"}e=e+"("+d.status+":"+i+")";break;case plupload.SECURITY_ERROR:e="安全配置错误。请联系网站管理员。";break;case plupload.GENERIC_ERROR:e="上传失败。请稍后再试。";break;case plupload.IO_ERROR:e="上传失败。请稍后再试。";break;case plupload.INIT_ERROR:e="网站配置错误。请联系网站管理员。",k.destroy();break;default:e=d.message+d.details}b&&b(c,d,e)}c.refresh()}}(d)),k.bind("FileUploaded",function(c){return function(d,e,g){var h=function(d,e,f){if(b.downtoken_url){var g=a.createAjax();g.open("POST",b.downtoken_url,!0),g.setRequestHeader("Content-type","application/x-www-form-urlencoded"),g.onreadystatechange=function(){if(4===g.readyState)if(200===g.status){var b;try{b=a.parseJSON(g.responseText)}catch(h){throw"invalid json format"}var i={};plupload.extend(i,a.parseJSON(f),b),c&&c(d,e,JSON.stringify(i))}else k.trigger("Error",{status:g.status,response:g.responseText,file:e,code:plupload.HTTP_ERROR})},g.send("key="+a.parseJSON(f).key+"&domain="+b.domain)}else c&&c(d,e,f)},i=a.parseJSON(g.response);if(f=f?f:i.ctx){var l="";b.save_key||(l=j(d,e,a.key_handler),l=l?"/key/"+a.URLSafeBase64Encode(l):"");var m=b.x_vars,n="",o="";if(void 0!==m&&"object"==typeof m)for(var p in m)m.hasOwnProperty(p)&&("function"==typeof m[p]?n=a.URLSafeBase64Encode(m[p](d,e)):"object"!=typeof m[p]&&(n=a.URLSafeBase64Encode(m[p])),o+="/x:"+p+"/"+n);var q="http://upload.qiniu.com/mkfile/"+e.size+l+o,r=a.createAjax();r.open("POST",q,!0),r.setRequestHeader("Content-Type","text/plain;charset=UTF-8"),r.setRequestHeader("Authorization","UpToken "+a.token),r.onreadystatechange=function(){if(4===r.readyState)if(localStorage.removeItem(e.name),200===r.status){var a=r.responseText;h(d,e,a)}else k.trigger("Error",{status:r.status,response:r.responseText,file:e,code:-200})},r.send(f)}else h(d,e,g.response)}}(e)),k},this.getUrl=function(a){if(!a)return!1;a=encodeURI(a);var b=this.domain;return"/"!==b.slice(b.length-1)&&(b+="/"),b+a},this.imageView2=function(a,b){var c=a.mode||"",d=a.w||"",e=a.h||"",f=a.q||"",g=a.format||"";if(!c)return!1;if(!d&&!e)return!1;var h="imageView2/"+c;return h+=d?"/w/"+d:"",h+=e?"/h/"+e:"",h+=f?"/q/"+f:"",h+=g?"/format/"+g:"",b&&(h=this.getUrl(b)+"?"+h),h},this.imageMogr2=function(a,b){var c=a["auto-orient"]||"",d=a.thumbnail||"",e=a.strip||"",f=a.gravity||"",g=a.crop||"",h=a.quality||"",i=a.rotate||"",j=a.format||"",k=a.blur||"",l="imageMogr2";return l+=c?"/auto-orient":"",l+=d?"/thumbnail/"+d:"",l+=e?"/strip":"",l+=f?"/gravity/"+f:"",l+=h?"/quality/"+h:"",l+=g?"/crop/"+g:"",l+=i?"/rotate/"+i:"",l+=j?"/format/"+j:"",l+=k?"/blur/"+k:"",b&&(l=this.getUrl(b)+"?"+l),l},this.watermark=function(a,b){var c=a.mode;if(!c)return!1;var d="watermark/"+c;if(1===c){var e=a.image||"";if(!e)return!1;d+=e?"/image/"+this.URLSafeBase64Encode(e):""}else{if(2!==c)return!1;var f=a.text?a.text:"",g=a.font?a.font:"",h=a.fontsize?a.fontsize:"",i=a.fill?a.fill:"";if(!f)return!1;d+=f?"/text/"+this.URLSafeBase64Encode(f):"",d+=g?"/font/"+this.URLSafeBase64Encode(g):"",d+=h?"/fontsize/"+h:"",d+=i?"/fill/"+this.URLSafeBase64Encode(i):""}var j=a.dissolve||"",k=a.gravity||"",l=a.dx||"",m=a.dy||"";return d+=j?"/dissolve/"+j:"",d+=k?"/gravity/"+k:"",d+=l?"/dx/"+l:"",d+=m?"/dy/"+m:"",b&&(d=this.getUrl(b)+"?"+d),d},this.imageInfo=function(a){if(!a)return!1;var b,c=this.getUrl(a)+"?imageInfo",d=this.createAjax(),e=this;return d.open("GET",c,!1),d.onreadystatechange=function(){4===d.readyState&&200===d.status&&(b=e.parseJSON(d.responseText))},d.send(),b},this.exif=function(a){if(!a)return!1;var b,c=this.getUrl(a)+"?exif",d=this.createAjax(),e=this;return d.open("GET",c,!1),d.onreadystatechange=function(){4===d.readyState&&200===d.status&&(b=e.parseJSON(d.responseText))},d.send(),b},this.get=function(a,b){return b&&a?"exif"===a?this.exif(b):"imageInfo"===a?this.imageInfo(b):!1:!1},this.pipeline=function(a,b){var c,d,e="[object Array]"===Object.prototype.toString.call(a),f="";if(e){for(var g=0,h=a.length;h>g;g++){if(c=a[g],!c.fop)return!1;switch(c.fop){case"watermark":f+=this.watermark(c)+"|";break;case"imageView2":f+=this.imageView2(c)+"|";break;case"imageMogr2":f+=this.imageMogr2(c)+"|";break;default:d=!0}if(d)return!1}if(b){f=this.getUrl(b)+"?"+f;var i=f.length;"|"===f.slice(i-1)&&(f=f.slice(0,i-1))}return f}return!1}}var Qiniu=new QiniuJsSDK; \ No newline at end of file +function QiniuJsSDK(){this.detectIEVersion=function(){for(var a=4,b=document.createElement("div"),c=b.getElementsByTagName("i");b.innerHTML="",c[0];)a++;return a>4?a:!1},this.isImage=function(a){var b,c="",d=["png","jpg","jpeg","gif","bmp"],e=/\.([a-zA-Z0-9]+)(\?|\@|$)/;if(!a||!e.test(a))return!1;b=e.exec(a),c=b[1].toLowerCase();for(var f=0,g=d.length;g>f;f++)if(c===d[f])return!0;return!1},this.getFileExtension=function(a){var b,c=a.split(".");return b=1===c.length||""===c[0]&&2===c.length?"":c.pop().toLowerCase()},this.utf8_encode=function(a){if(null===a||"undefined"==typeof a)return"";var b,c,d=a+"",e="",f=0;b=c=0,f=d.length;for(var g=0;f>g;g++){var h=d.charCodeAt(g),i=null;if(128>h)c++;else if(h>127&&2048>h)i=String.fromCharCode(h>>6|192,63&h|128);else if(63488&h^!0)i=String.fromCharCode(h>>12|224,h>>6&63|128,63&h|128);else{if(64512&h^!0)throw new RangeError("Unmatched trail surrogate at "+g);var j=d.charCodeAt(++g);if(64512&j^!0)throw new RangeError("Unmatched lead surrogate at "+(g-1));h=((1023&h)<<10)+(1023&j)+65536,i=String.fromCharCode(h>>18|240,h>>12&63|128,h>>6&63|128,63&h|128)}null!==i&&(c>b&&(e+=d.slice(b,c)),e+=i,b=c=g+1)}return c>b&&(e+=d.slice(b,f)),e},this.base64_encode=function(a){var b,c,d,e,f,g,h,i,j="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",k=0,l=0,m="",n=[];if(!a)return a;a=this.utf8_encode(a+"");do b=a.charCodeAt(k++),c=a.charCodeAt(k++),d=a.charCodeAt(k++),i=b<<16|c<<8|d,e=i>>18&63,f=i>>12&63,g=i>>6&63,h=63&i,n[l++]=j.charAt(e)+j.charAt(f)+j.charAt(g)+j.charAt(h);while(k=f&&b.chunk_size&&b.runtimes.indexOf("flash")>=0?b.chunk_size=0:g?b.chunk_size=0:(c=20,d=4<d&&(b.chunk_size=d))};h();var i=function(){if(b.uptoken)a.token=b.uptoken;else{var c=a.createAjax();c.open("GET",a.uptoken_url,!0),c.setRequestHeader("If-Modified-Since","0"),c.onreadystatechange=function(){if(4===c.readyState&&200===c.status){var b=a.parseJSON(c.responseText);a.token=b.uptoken}},c.send()}},j=function(c,d,e){var f="",g=!1;if(!b.save_key)if(g=c.getOption&&c.getOption("unique_names"),g=g||c.settings&&c.settings.unique_names){var h=a.getFileExtension(d.name);f=h?d.id+"."+h:d.id}else f="function"==typeof e?e(c,d):d.name;return f};plupload.extend(c,b,{url:"http://upload.qiniu.com",multipart_params:{token:""}});var k=new plupload.Uploader(c);return k.bind("Init",function(a,b){i()}),k.init(),k.bind("FilesAdded",function(a,b){var c=a.getOption&&a.getOption("auto_start");c=c||a.settings&&a.settings.auto_start,c&&plupload.each(b,function(b,c){a.start()}),a.refresh()}),k.bind("BeforeUpload",function(c,d){d.speed=d.speed||0,f="";var e=function(c,d,e){g.startTime=(new Date).getTime();var f;f=b.save_key?{token:a.token}:{key:j(c,d,e),token:a.token};var h=b.x_vars;if(void 0!==h&&"object"==typeof h)for(var i in h)h.hasOwnProperty(i)&&("function"==typeof h[i]?f["x:"+i]=h[i](c,d):"object"!=typeof h[i]&&(f["x:"+i]=h[i]));c.setOption({url:"http://upload.qiniu.com/",multipart:!0,chunk_size:void 0,multipart_params:f})},h=c.getOption&&c.getOption("chunk_size");if(h=h||c.settings&&c.settings.chunk_size,"html5"===k.runtime&&h)if(d.sizem-n&&100!==i.percent&&d.size===i.total?(d.percent=i.percent,d.loaded=i.offset,f=i.ctx,g.isResumeUpload=!0,g.resumeFilesize=i.offset,i.offset+l>d.size&&(l=d.size-i.offset)):localStorage.removeItem(d.name)}g.startTime=(new Date).getTime(),c.setOption({url:"http://upload.qiniu.com/mkblk/"+l,multipart:!1,chunk_size:h,required_features:"chunks",headers:{Authorization:"UpToken "+a.token},multipart_params:{}})}else e(c,d,a.key_handler)}),k.bind("UploadProgress",function(a,b){g.currentTime=(new Date).getTime();var c=g.currentTime-g.startTime,d=b.loaded||0;g.isResumeUpload&&(d=b.loaded-g.resumeFilesize),b.speed=(d/c*1e3).toFixed(0)||0}),k.bind("ChunkUploaded",function(b,c,d){var e=a.parseJSON(d.response);f=f?f+","+e.ctx:e.ctx;var g=d.total-d.offset,h=b.getOption&&b.getOption("chunk_size");h=h||b.settings&&b.settings.chunk_size,h>g&&b.setOption({url:"http://upload.qiniu.com/mkblk/"+g}),localStorage.setItem(c.name,JSON.stringify({ctx:f,percent:c.percent,total:d.total,offset:d.offset,time:(new Date).getTime()}))}),k.bind("Error",function(b){return function(c,d){var e="",f=d.file;if(f){switch(d.code){case plupload.FAILED:e="上传失败。请稍后再试。";break;case plupload.FILE_SIZE_ERROR:var g=c.getOption&&c.getOption("max_file_size");g=g||c.settings&&c.settings.max_file_size,e="浏览器最大可上传"+g+"。更大文件请使用命令行工具。";break;case plupload.FILE_EXTENSION_ERROR:e="文件验证失败。请稍后重试。";break;case plupload.HTTP_ERROR:var h=a.parseJSON(d.response),i=h.error;switch(d.status){case 400:e="请求报文格式错误。";break;case 401:e="客户端认证授权失败。请重试或提交反馈。";break;case 405:e="客户端请求错误。请重试或提交反馈。";break;case 579:e="资源上传成功,但回调失败。";break;case 599:e="网络连接异常。请重试或提交反馈。";break;case 614:e="文件已存在。";try{h=a.parseJSON(h.error),i=h.error||"file exists"}catch(j){i=h.error||"file exists"}break;case 631:e="指定空间不存在。";break;case 701:e="上传数据块校验出错。请重试或提交反馈。";break;default:e="未知错误。"}e=e+"("+d.status+":"+i+")";break;case plupload.SECURITY_ERROR:e="安全配置错误。请联系网站管理员。";break;case plupload.GENERIC_ERROR:e="上传失败。请稍后再试。";break;case plupload.IO_ERROR:e="上传失败。请稍后再试。";break;case plupload.INIT_ERROR:e="网站配置错误。请联系网站管理员。",k.destroy();break;default:e=d.message+d.details}b&&b(c,d,e)}c.refresh()}}(d)),k.bind("FileUploaded",function(c){return function(d,e,g){var h=function(d,e,f){if(b.downtoken_url){var g=a.createAjax();g.open("POST",b.downtoken_url,!0),g.setRequestHeader("Content-type","application/x-www-form-urlencoded"),g.onreadystatechange=function(){if(4===g.readyState)if(200===g.status){var b;try{b=a.parseJSON(g.responseText)}catch(h){throw"invalid json format"}var i={};plupload.extend(i,a.parseJSON(f),b),c&&c(d,e,JSON.stringify(i))}else k.trigger("Error",{status:g.status,response:g.responseText,file:e,code:plupload.HTTP_ERROR})},g.send("key="+a.parseJSON(f).key+"&domain="+b.domain)}else c&&c(d,e,f)},i=a.parseJSON(g.response);if(f=f?f:i.ctx){var l="";b.save_key||(l=j(d,e,a.key_handler),l=l?"/key/"+a.URLSafeBase64Encode(l):"");var m=b.x_vars,n="",o="";if(void 0!==m&&"object"==typeof m)for(var p in m)m.hasOwnProperty(p)&&("function"==typeof m[p]?n=a.URLSafeBase64Encode(m[p](d,e)):"object"!=typeof m[p]&&(n=a.URLSafeBase64Encode(m[p])),o+="/x:"+p+"/"+n);var q="http://upload.qiniu.com/mkfile/"+e.size+l+o,r=a.createAjax();r.open("POST",q,!0),r.setRequestHeader("Content-Type","text/plain;charset=UTF-8"),r.setRequestHeader("Authorization","UpToken "+a.token),r.onreadystatechange=function(){if(4===r.readyState)if(localStorage.removeItem(e.name),200===r.status){var a=r.responseText;h(d,e,a)}else k.trigger("Error",{status:r.status,response:r.responseText,file:e,code:-200})},r.send(f)}else h(d,e,g.response)}}(e)),k},this.getUrl=function(a){if(!a)return!1;a=encodeURI(a);var b=this.domain;return"/"!==b.slice(b.length-1)&&(b+="/"),b+a},this.imageView2=function(a,b){var c=a.mode||"",d=a.w||"",e=a.h||"",f=a.q||"",g=a.format||"";if(!c)return!1;if(!d&&!e)return!1;var h="imageView2/"+c;return h+=d?"/w/"+d:"",h+=e?"/h/"+e:"",h+=f?"/q/"+f:"",h+=g?"/format/"+g:"",b&&(h=this.getUrl(b)+"?"+h),h},this.imageMogr2=function(a,b){var c=a["auto-orient"]||"",d=a.thumbnail||"",e=a.strip||"",f=a.gravity||"",g=a.crop||"",h=a.quality||"",i=a.rotate||"",j=a.format||"",k=a.blur||"",l="imageMogr2";return l+=c?"/auto-orient":"",l+=d?"/thumbnail/"+d:"",l+=e?"/strip":"",l+=f?"/gravity/"+f:"",l+=h?"/quality/"+h:"",l+=g?"/crop/"+g:"",l+=i?"/rotate/"+i:"",l+=j?"/format/"+j:"",l+=k?"/blur/"+k:"",b&&(l=this.getUrl(b)+"?"+l),l},this.watermark=function(a,b){var c=a.mode;if(!c)return!1;var d="watermark/"+c;if(1===c){var e=a.image||"";if(!e)return!1;d+=e?"/image/"+this.URLSafeBase64Encode(e):""}else{if(2!==c)return!1;var f=a.text?a.text:"",g=a.font?a.font:"",h=a.fontsize?a.fontsize:"",i=a.fill?a.fill:"";if(!f)return!1;d+=f?"/text/"+this.URLSafeBase64Encode(f):"",d+=g?"/font/"+this.URLSafeBase64Encode(g):"",d+=h?"/fontsize/"+h:"",d+=i?"/fill/"+this.URLSafeBase64Encode(i):""}var j=a.dissolve||"",k=a.gravity||"",l=a.dx||"",m=a.dy||"";return d+=j?"/dissolve/"+j:"",d+=k?"/gravity/"+k:"",d+=l?"/dx/"+l:"",d+=m?"/dy/"+m:"",b&&(d=this.getUrl(b)+"?"+d),d},this.imageInfo=function(a){if(!a)return!1;var b,c=this.getUrl(a)+"?imageInfo",d=this.createAjax(),e=this;return d.open("GET",c,!1),d.onreadystatechange=function(){4===d.readyState&&200===d.status&&(b=e.parseJSON(d.responseText))},d.send(),b},this.exif=function(a){if(!a)return!1;var b,c=this.getUrl(a)+"?exif",d=this.createAjax(),e=this;return d.open("GET",c,!1),d.onreadystatechange=function(){4===d.readyState&&200===d.status&&(b=e.parseJSON(d.responseText))},d.send(),b},this.get=function(a,b){return b&&a?"exif"===a?this.exif(b):"imageInfo"===a?this.imageInfo(b):!1:!1},this.pipeline=function(a,b){var c,d,e="[object Array]"===Object.prototype.toString.call(a),f="";if(e){for(var g=0,h=a.length;h>g;g++){if(c=a[g],!c.fop)return!1;switch(c.fop){case"watermark":f+=this.watermark(c)+"|";break;case"imageView2":f+=this.imageView2(c)+"|";break;case"imageMogr2":f+=this.imageMogr2(c)+"|";break;default:d=!0}if(d)return!1}if(b){f=this.getUrl(b)+"?"+f;var i=f.length;"|"===f.slice(i-1)&&(f=f.slice(0,i-1))}return f}return!1}}var Qiniu=new QiniuJsSDK; \ No newline at end of file From bf9a6732d178b6ed28af508fc2602314fec867fe Mon Sep 17 00:00:00 2001 From: meilixie Date: Fri, 14 Aug 2015 10:16:50 +0800 Subject: [PATCH 2/5] cancel upload file finished --- demo/js/ui.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/demo/js/ui.js b/demo/js/ui.js index 733c8cdc..e308c099 100644 --- a/demo/js/ui.js +++ b/demo/js/ui.js @@ -358,8 +358,9 @@ FileProgress.prototype.setCancelled = function(manual) { progressContainer += ' red'; } this.fileProgressWrapper.attr('class', progressContainer); - this.fileProgressWrapper.find('td .progress .progress-bar-info').css('width', 0); + this.fileProgressWrapper.find('td .progress').remove(); this.fileProgressWrapper.find('td:eq(2) .btn-default').hide(); + this.fileProgressWrapper.find('td:eq(2) .progressCancel').hide(); }; FileProgress.prototype.setStatus = function(status, isUploading) { @@ -375,9 +376,8 @@ FileProgress.prototype.uploadCancel = function(up) { self.fileProgressWrapper.find('td:eq(2) .progressCancel').on('click', function(){ self.setCancelled(false); self.setStatus("取消上传"); - // var status_before = self.file.status; + self.fileProgressWrapper.find('.status').css('left', '0'); up.removeFile(self.file); - up.stop(); return true; }); } From ba785d0f94203233f56e30b3dda80a45fb158037 Mon Sep 17 00:00:00 2001 From: meilixie Date: Fri, 14 Aug 2015 10:33:53 +0800 Subject: [PATCH 3/5] rename cancelupload -> bindCancelUpload --- demo/js/main.js | 8 +++----- demo/js/ui.js | 3 ++- demo/main.css | 2 ++ 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/demo/js/main.js b/demo/js/main.js index c0ca6241..0a8b9f8d 100644 --- a/demo/js/main.js +++ b/demo/js/main.js @@ -35,7 +35,7 @@ $(function() { plupload.each(files, function(file) { var progress = new FileProgress(file, 'fsUploadProgress'); progress.setStatus("等待..."); - progress.uploadCancel(up); + progress.bindUploadCancel(up); }); }, 'BeforeUpload': function(up, file) { @@ -44,15 +44,13 @@ $(function() { if (up.runtime === 'html5' && chunk_size) { progress.setChunkProgess(chunk_size); } - progress.uploadCancel(up); + progress.bindUploadCancel(up); }, 'UploadProgress': function(up, file) { var progress = new FileProgress(file, 'fsUploadProgress'); - var chunk_size = plupload.parseSize(this.getOption('chunk_size')); - progress.setProgress(file.percent + "%", file.speed, chunk_size); - progress.uploadCancel(up); + progress.bindUploadCancel(up); }, 'UploadComplete': function() { diff --git a/demo/js/ui.js b/demo/js/ui.js index e308c099..54a5e7be 100644 --- a/demo/js/ui.js +++ b/demo/js/ui.js @@ -369,7 +369,8 @@ FileProgress.prototype.setStatus = function(status, isUploading) { } }; -FileProgress.prototype.uploadCancel = function(up) { +// 绑定取消上传事件 +FileProgress.prototype.bindUploadCancel = function(up) { var self = this; if (up) { diff --git a/demo/main.css b/demo/main.css index 98e36abf..b32d014b 100644 --- a/demo/main.css +++ b/demo/main.css @@ -72,6 +72,8 @@ tr .progressCancel{ float: right; position: relative; z-index: 100; + color: #333; + text-decoration: none; } .status { position: absolute; From 3629ad41c9bd91f9fa5118850f6981db56afd23d Mon Sep 17 00:00:00 2001 From: meilixie Date: Fri, 14 Aug 2015 10:49:52 +0800 Subject: [PATCH 4/5] add .travis.yml file to make ci pass --- .travis.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..8de39eb1 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,10 @@ +language: node_js +node_js: + - "0.12" + - "iojs" + - "iojs-v1.0.4" + + +before_script: + - npm install -g gulp +script: gulp \ No newline at end of file From 138ad33fd7f4fd3ac64958fdd41e1044622841fa Mon Sep 17 00:00:00 2001 From: meilixie Date: Fri, 14 Aug 2015 10:54:58 +0800 Subject: [PATCH 5/5] delete gulp part in .travis.yml file --- .travis.yml | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index 8de39eb1..3d3b8d95 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,9 +2,4 @@ language: node_js node_js: - "0.12" - "iojs" - - "iojs-v1.0.4" - - -before_script: - - npm install -g gulp -script: gulp \ No newline at end of file + - "iojs-v1.0.4" \ No newline at end of file