forked from weixiyen/jquery-filedrop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.filedrop.min.js
13 lines (13 loc) · 6.14 KB
/
jquery.filedrop.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
/* Author: Weixi Yen - Copyright (c) 2010 Resopollution - Licensed under the MIT license - http://www.github.com/weixiyen/jquery-filedrop */
(function(m){function d(){}jQuery.event.props.push("dataTransfer");var B={fallback_id:"",url:"",refresh:1E3,paramname:"userfile",requestType:"POST",allowedfileextensions:[],allowedfiletypes:[],maxfiles:25,maxfilesize:1,queuefiles:0,queuewait:200,data:{},headers:{},drop:d,dragStart:d,dragEnter:d,dragOver:d,dragLeave:d,docEnter:d,docOver:d,docLeave:d,beforeEach:d,afterAll:d,rename:d,error:function(d,f,m,t){alert(d)},uploadStarted:d,uploadFinished:d,progressUpdated:d,globalProgressUpdated:d,speedUpdated:d},
f="BrowserNotSupported TooManyFiles FileTooLarge FileTypeNotAllowed NotFound NotReadable AbortError ReadError FileExtensionNotAllowed".split(" ");m.fn.filedrop=function(d){function z(a,e,d,f){var c="",k=b.paramname;if(b.data){var n=m.param(b.data).replace(/\+/g,"%20").split(/&/);m.each(n,function(){var b=this.split("=",2),a=decodeURIComponent(b[0]),e=decodeURIComponent(b[1]);2===b.length&&(c+="--",c+=f,c+="\r\n",c+='Content-Disposition: form-data; name="'+a+'"',c+="\r\n",c+="\r\n",c+=e,c+="\r\n")})}jQuery.isFunction(k)&&
(k=k(a));c+="--";c+=f;c+="\r\n";c+='Content-Disposition: form-data; name="'+(k||"")+'"';c+='; filename="'+encodeURIComponent(a)+'"';c+="\r\n";c+="Content-Type: "+d;c+="\r\n";c+="\r\n";c+=e;c+="\r\n";c+="--";c+=f;c+="--";return c+="\r\n"}function A(a){if(a.lengthComputable){var e=Math.round(100*a.loaded/a.total);if(this.currentProgress!==e){this.currentProgress=e;b.progressUpdated(this.index,this.file,this.currentProgress);k[this.global_progress_index]=this.currentProgress;t();var e=(new Date).getTime(),
d=e-this.currentStart;d>=b.refresh&&(b.speedUpdated(this.index,this.file,(a.loaded-this.startData)/d),this.startData=a.loaded,this.currentStart=e)}}}function t(){if(0!==k.length){var a=0,e;for(e in k)k.hasOwnProperty(e)&&(a+=k[e]);b.globalProgressUpdated(Math.round(a/k.length))}}function u(){w=!1;if(!e)return b.error(f[0]),!1;if(b.allowedfiletypes.push&&b.allowedfiletypes.length)for(var a=e.length;a--;)if(!e[a].type||0>m.inArray(e[a].type,b.allowedfiletypes))return b.error(f[3],e[a]),!1;if(b.allowedfileextensions.push&&
b.allowedfileextensions.length)for(a=e.length;a--;){for(var d=!1,l=0;l<b.allowedfileextensions.length;l++)e[a].name.substr(e[a].name.length-b.allowedfileextensions[l].length).toLowerCase()==b.allowedfileextensions[l].toLowerCase()&&(d=!0);if(!d)return b.error(f[8],e[a]),!1}var y=0,c=0;if(p>b.maxfiles&&0===b.queuefiles)return b.error(f[1]),!1;for(var h=[],n=[],u=[],l=0;l<p;l++)h.push(l);var q=function(){var a;if(w)return!1;if(0<b.queuefiles&&n.length>=b.queuefiles)setTimeout(q,b.queuewait);else{a=
h[0];h.splice(0,1);n.push(a);try{if(!1!==b.beforeEach(e[a])){if(a===p)return;var d=new FileReader,g=1048576*b.maxfilesize;d.index=a;if(e[a].size>g)return b.error(f[2],e[a],a),n.forEach(function(b,c){b===a&&n.splice(c,1)}),c++,!0;d.onerror=function(a){switch(a.target.error.code){case a.target.error.NOT_FOUND_ERR:return b.error(f[4]),!1;case a.target.error.NOT_READABLE_ERR:return b.error(f[5]),!1;case a.target.error.ABORT_ERR:return b.error(f[6]),!1;default:return b.error(f[7]),!1}};d.onloadend=b.beforeSend?
function(c){b.beforeSend(e[a],a,function(){r(c)})}:r;d.readAsDataURL(e[a])}else c++}catch(k){return n.forEach(function(b,c){b===a&&n.splice(c,1)}),b.error(f[0]),!1}0<h.length&&q()}},r=function(a){var d=(a.srcElement||a.target).index;void 0===a.target.index&&(a.target.index=C(a.total));var g=new XMLHttpRequest,f=g.upload,h=e[a.target.index],l=a.target.index,v=(new Date).getTime(),q="------multipartformboundary"+(new Date).getTime(),x=k.length,s;s=b.rename(h.name);var r=h.type;a=atob(a.target.result.split(",")[1]);
s="string"===typeof s?z(s,a,r,q):z(h.name,a,r,q);f.index=l;f.file=h;f.downloadStartTime=v;f.currentStart=v;f.currentProgress=0;f.global_progress_index=x;f.startData=0;f.addEventListener("progress",A,!1);jQuery.isFunction(b.url)?g.open(b.requestType,b.url(),!0):g.open(b.requestType,b.url,!0);b.withCredentials&&(g.withCredentials=b.withCredentials);g.setRequestHeader("content-type","multipart/form-data; boundary="+q);g.setRequestHeader("X-Requested-With","XMLHttpRequest");m.each(b.headers,function(a,
b){g.setRequestHeader(a,b)});g.sendAsBinary||(g.sendAsBinary=function(a){a=Array.prototype.map.call(a,function(a){return a.charCodeAt(0)&255});a=new Uint8Array(a);this.send(a.buffer)});g.sendAsBinary(s);k[x]=0;t();b.uploadStarted(l,h,p);g.onload=function(){var a=null;if(g.responseText)try{a=jQuery.parseJSON(g.responseText)}catch(e){a=g.responseText}var f=(new Date).getTime()-v,a=b.uploadFinished(l,h,a,f,g);y++;n.forEach(function(a,b){a===d&&n.splice(b,1)});u.push(d);k[x]=100;t();y===p-c&&b.afterAll();
!1===a&&(w=!0);(200>g.status||299<g.status)&&b.error(g.statusText,h,d,g.status)}};q()}function C(a){for(var b=0;b<p;b++)if(e[b].size===a)return b}var b=m.extend({},B,d),k=[],h,w=!1,p=0,e;this.on("drop",function(a){if(!1===b.drop.call(this,a))return!1;if(a.dataTransfer){e=a.dataTransfer.files;if(null===e||void 0===e||0===e.length)return b.error(f[0]),!1;p=e.length;u();a.preventDefault();return!1}}).on("dragstart",b.dragStart).on("dragenter",function(a){clearTimeout(h);a.preventDefault();b.dragEnter.call(this,
a)}).on("dragover",function(a){clearTimeout(h);a.preventDefault();b.docOver.call(this,a);b.dragOver.call(this,a)}).on("dragleave",function(a){clearTimeout(h);b.dragLeave.call(this,a);a.stopPropagation()});m(document).on("drop",function(a){a.preventDefault();b.docLeave.call(this,a);return!1}).on("dragenter",function(a){clearTimeout(h);a.preventDefault();b.docEnter.call(this,a);return!1}).on("dragover",function(a){clearTimeout(h);a.preventDefault();b.docOver.call(this,a);return!1}).on("dragleave",function(a){h=
setTimeout(function(e){return function(){b.docLeave.call(e,a)}}(this),200)});this.on("click",function(a){m("#"+b.fallback_id).trigger(a)});m("#"+b.fallback_id).change(function(a){b.drop(a);e=a.target.files;p=e.length;u()});return this};try{XMLHttpRequest.prototype.sendAsBinary||(XMLHttpRequest.prototype.sendAsBinary=function(d){d=Array.prototype.map.call(d,function(d){return d.charCodeAt(0)&255});d=new Uint8Array(d);"ArrayBufferView"in window?this.send(d):this.send(d.buffer)})}catch(D){}})(jQuery);