This repository was archived by the owner on Apr 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnoswfupload.min.js
More file actions
8 lines (8 loc) · 11.5 KB
/
noswfupload.min.js
File metadata and controls
8 lines (8 loc) · 11.5 KB
1
2
3
4
5
6
7
8
/** No SWF Object Multiple File Upload
* @version 1.1.1
* @compatibility Chrome 1, FireFox 3+, Internet Explorer 5+, Opera 8+, Safari 3+
* @author Andrea Giammarchi
* @blog webreflection.blogspot.com
* @license Mit Style License
*/
(function(className){var $={css:function(css,icons){var head=document.getElementsByTagName("head")[0]||document.documentElement,style=document.createElement("link");style.setAttribute("rel","stylesheet");style.setAttribute("type","text/css");style.setAttribute("media","all");style.setAttribute("href",css);head.insertBefore(style,head.firstChild);return icons?$.css(icons):$},event:{add:document.addEventListener?function(node,name,callback){node.addEventListener(name,callback,false);return this}:function(node,name,callback){node.attachEvent("on"+name,callback);return this},del:document.removeEventListener?function(node,name,callback){node.removeEventListener(name,callback,false);return this}:function(node,name,callback){node.detachEvent("on"+name,callback);return this},stop:function(e){if(!e){if(self.event){event.returnValue=!(event.cancelBubble=true)}}else{e.stopPropagation?e.stopPropagation():e.cancelBubble=true;e.preventDefault?e.preventDefault():e.returnValue=false}return false}},events:function(wrap){$.event.add(wrap.dom.input,"change",function(){lastInput=wrap.dom.input;$.event.del(wrap.dom.input,"change",arguments.callee);for(var max=false,input=wrap.dom.input.cloneNode(true),i=0,files=$.files(wrap.dom.input);i<files.length;i++){var value=files.item(i).fileName||(files.item(i).fileName=files.item(i).value.split("\\").pop()),ext=-1!==value.indexOf(".")?value.split(".").pop().toLowerCase():"unknown";if(wrap.fileType&&-1===wrap.fileType.indexOf("*."+ext)){max=true;$.text(wrap.dom.info,$.lang.errorType.replace(/{fileType}/g,wrap.fileType).replace(/{fileName}/g,value))}else{if(wrap.maxSize!==-1&&files.item(i).fileSize&&wrap.maxSize<files.item(i).fileSize){max=true;$.text(wrap.dom.info,$.lang.errorSize.replace(/{maxSize}/g,$.size(wrap.maxSize)).replace(/{fileName}/g,value).replace(/{fileSize}/g,$.size(files.item(i).fileSize)))}else{var li=document.createElement("li"),a=li.appendChild(document.createElement("a"));wrap.files.unshift(files.item(i));a.href="#"+value;a.alt=a.title=(files.item(i).fileSize?"["+$.size(files.item(i).fileSize)+"] ":"")+value;li.className=ext;$.text(a,value);$.event.add(a,"click",$.empty).add(a,"dblclick",files.item(i).remove=(function(li){return function(e){for(var i=0,childNodes=li.parentNode.getElementsByTagName("li");i<childNodes.length;i++){if(childNodes[i]===li){var value=wrap.files[i].fileName;$.event.del(li,"click",$.empty).del(li,"dblclick",arguments.callee);li.parentNode.removeChild(li);li=a=null;wrap.files.splice(i,1);$.text(wrap.dom.info,$.lang.removedFile.replace(/{fileName}/g,value));break}}return $.empty(e)}})(li));if(typeof files.item(i).fileSize!="number"){files.item(i).fileSize=-1}if(wrap.dom.ul.firstChild){wrap.dom.ul.insertBefore(li,wrap.dom.ul.firstChild)}else{wrap.dom.ul.appendChild(li)}}}}input.value="";wrap.dom.input.parentNode.replaceChild(input,wrap.dom.input);wrap.dom.input=input;if(!max){$.text(wrap.dom.info,$.lang.removeFile)}$.event.add(wrap.dom.input,"change",arguments.callee)});return wrap},empty:function(e){return $.event.stop(e)},files:(function(item){return function(input){var files=input.files||[input];if(!files.item){files.item=item}return files}})(function(i){return this[i]}),lang:{removeFile:"Double click to remove a file",removedFile:"File {fileName} removed",errorSize:"WARNING - Maximum size is {maxSize}. File {fileName} is {fileSize}",errorType:"WARNING - File {fileName} has not a valid type: {fileType}"},sendFile:(function(toString){var multipart=function(boundary,name,file){return"--".concat(boundary,CRLF,'Content-Disposition: form-data; name="',name,'"; filename="',file.fileName,'"',CRLF,"Content-Type: application/octet-stream",CRLF,CRLF,file.getAsBinary(),CRLF,"--",boundary,"--",CRLF)},isFunction=function(Function){return toString.call(Function)==="[object Function]"},split="onabort.onerror.onloadstart.onprogress".split("."),length=split.length,CRLF="\r\n",xhr=this.XMLHttpRequest?new XMLHttpRequest:new ActiveXObject("Microsoft.XMLHTTP"),sendFile;if(xhr.upload||xhr.sendAsBinary){sendFile=function(handler,maxSize){if(-1<maxSize&&maxSize<handler.file.fileSize){if(isFunction(handler.onerror)){handler.onerror()}return}for(var xhr=new XMLHttpRequest,upload=xhr.upload||{addEventListener:function(event,callback){this["on"+event]=callback}},i=0;i<length;i++){upload.addEventListener(split[i].substring(2),(function(event){return function(rpe){if(isFunction(handler[event])){handler[event](rpe,xhr)}}})(split[i]),false)}upload.addEventListener("load",function(rpe){if(handler.onreadystatechange===false){if(isFunction(handler.onload)){handler.onload(rpe,xhr)}}else{setTimeout(function(){if(xhr.readyState===4){if(isFunction(handler.onload)){handler.onload(rpe,xhr)}}else{setTimeout(arguments.callee,15)}},15)}},false);xhr.open("post",handler.url,true);if(!xhr.upload){var rpe={loaded:0,total:handler.file.fileSize,simulation:true};rpe.interval=setInterval(function(){rpe.loaded+=1024/4;if(rpe.total<=rpe.loaded){rpe.loaded=rpe.total}upload.onprogress(rpe)},100);xhr.onabort=function(){upload.onabort({})};xhr.onerror=function(){upload.onerror({})};xhr.onreadystatechange=function(){switch(xhr.readyState){case 2:case 3:if(rpe.total<=rpe.loaded){rpe.loaded=rpe.total}upload.onprogress(rpe);break;case 4:clearInterval(rpe.interval);rpe.interval=0;rpe.loaded=rpe.total;upload.onprogress(rpe);upload[199<xhr.status&&xhr.status<400?"onload":"onerror"]({});break}};upload.onloadstart(rpe)}if(handler.file.getAsBinary){var boundary="AjaxUploadBoundary"+(new Date).getTime();xhr.setRequestHeader("Content-Type","multipart/form-data; boundary="+boundary);xhr[xhr.sendAsBinary?"sendAsBinary":"send"](multipart(boundary,handler.name,handler.file))}else{xhr.setRequestHeader("Content-Type","multipart/form-data");xhr.setRequestHeader("X-Name",handler.name);xhr.setRequestHeader("X-Filename",handler.file.fileName);xhr.send(handler.file)}return handler}}else{sendFile=function(handler,maxSize){var url=handler.url.concat(-1===handler.url.indexOf("?")?"?":"&","AjaxUploadFrame=true"),rpe={loaded:1,total:100,simulation:true,interval:setInterval(function(){if(rpe.loaded<rpe.total){++rpe.loaded}if(isFunction(handler.onprogress)){handler.onprogress(rpe,{})}},100)},onload=function(){iframe.onreadystatechange=iframe.onload=iframe.onerror=null;form.parentNode.removeChild(form);form=null;clearInterval(rpe.interval);rpe.loaded=rpe.total;try{var responseText=(iframe.contentWindow.document||iframe.contentWindow.contentDocument).body.innerHTML;if(isFunction(handler.onprogress)){handler.onprogress(rpe,{})}if(isFunction(handler.onload)){handler.onload(rpe,{responseText:responseText})}}catch(e){if(isFunction(handler.onerror)){handler.onerror(rpe,event||window.event)}}},target=["AjaxUpload",(new Date).getTime(),String(Math.random()).substring(2)].join("_");try{var form=document.createElement('<form enctype="multipart/form-data"></form>'),iframe=handler.iframe||(handler.iframe=document.createElement('<iframe id="'+target+'" name="'+target+'" src="'+url+'"></iframe>'))}catch(e){var form=document.createElement("form"),iframe=handler.iframe||(handler.iframe=document.createElement("iframe"));form.setAttribute("enctype","multipart/form-data");iframe.setAttribute("name",iframe.id=target);iframe.setAttribute("src",url)}iframe.style.position="absolute";iframe.style.left=iframe.style.top="-10000px";iframe.onload=onload;iframe.onerror=function(event){if(isFunction(handler.onerror)){handler.onerror(rpe,event||window.event)}};iframe.onreadystatechange=function(){if(/loaded|complete/i.test(iframe.readyState)){onload()}else{if(isFunction(handler.onloadprogress)){if(rpe.loaded<rpe.total){++rpe.loaded}handler.onloadprogress(rpe,{readyState:{loading:2,interactive:3,loaded:4,complete:4}[iframe.readyState]||1})}}};form.setAttribute("action",handler.url);form.setAttribute("target",iframe.id);form.setAttribute("method","post");form.appendChild(handler.file);form.style.display="none";if(isFunction(handler.onloadstart)){handler.onloadstart(rpe,{})}with(document.body||document.documentElement){appendChild(iframe);appendChild(form).submit()}return handler}}xhr=null;return sendFile})(Object.prototype.toString),sendFiles:function(handler,maxSize){var length=handler.files.length,i=0,onload=handler.onload,onloadstart=handler.onloadstart;handler.current=0;handler.total=0;handler.sent=0;while(handler.current<length){handler.total+=handler.files[handler.current++].fileSize}handler.current=0;if(length&&handler.files[0].fileSize!==-1){handler.file=handler.files[handler.current];$.sendFile(handler,maxSize).onload=function(rpe,xhr){handler.onloadstart=null;handler.sent+=handler.files[handler.current].fileSize;if(++handler.current<length){handler.file=handler.files[handler.current];$.sendFile(handler,maxSize).onload=arguments.callee}else{if(onload){handler.onloadstart=onloadstart;handler.onload=onload;handler.onload(rpe,xhr)}}}}else{if(length){handler.total=length*100;handler.file=handler.files[handler.current];$.sendFile(handler,maxSize).onload=function(rpe,xhr){var callee=arguments.callee;handler.onloadstart=null;handler.sent+=100;if(++handler.current<length){if(/\b(chrome|safari)\b/i.test(navigator.userAgent)){handler.iframe.parentNode.removeChild(handler.iframe);handler.iframe=null}setTimeout(function(){handler.file=handler.files[handler.current];$.sendFile(handler,maxSize).onload=callee},15)}else{if(onload){setTimeout(function(){handler.iframe.parentNode.removeChild(handler.iframe);handler.iframe=null;handler.onloadstart=onloadstart;handler.onload=onload;handler.onload(rpe,xhr)},15)}}}}}return handler},size:(function(info){return function(bytes){var i=0;while(1023<bytes){bytes/=1024;++i}return(i?bytes.toFixed(2):bytes)+info[i]}})([" bytes"," Kb"," Mb"," Gb"," Tb"]),text:(function(slice){return function(node){arguments=slice.call(arguments,1);while(node.firstChild){node.removeChild(node.firstChild)}while(arguments.length){node.appendChild(document.createTextNode(arguments.shift()));if(arguments.length){node.appendChild(document.createElement("br"))}}}})(Array.prototype.slice),wrap:function(input,maxSize){var wrap=document.createElement("div"),total=document.createElement("span"),current=total.cloneNode(true),ul=document.createElement("ul"),info=total.cloneNode(true);input.setAttribute("multiple","multiple");with(wrap){appendChild(total);appendChild(current);appendChild(ul);appendChild(info)}wrap.className=className;total.className="total";current.className="current";info.className="info";total.style.visibility=current.style.visibility="hidden";wrap.insertBefore(input.parentNode.replaceChild(wrap,input),total);input.value="";return $.events({dom:{wrap:wrap,input:input,total:total,current:current,ul:ul,info:info,disabled:false},name:input.name,maxSize:maxSize?maxSize>>0:-1,files:[],clean:function(){while(this.files.length){this.files[0].remove()}},upload:function(handler){if(handler){for(var key in handler){this[key]=handler[key]}}$.sendFiles(this,this.maxSize);return this},hide:function(){if(this.dom.disabled){this.dom.disabled=false;this.dom.input.removeAttribute("disabled")}total.style.visibility=current.style.visibility="hidden"},show:function(total,current,totalProp,currentProp){var tstyle=this.dom.total.style,cstyle=this.dom.current.style;if(!this.dom.disabled){this.dom.disabled=true;this.dom.input.setAttribute("disabled","disabled");tstyle.visibility=cstyle.visibility=""}tstyle[totalProp||"height"]=(total>>0)+"px";cstyle[currentProp||"height"]=(current>>0)+"px"}})}};this[className]=$})("noswfupload");