forked from rodrigowbazevedo/simpleUpload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpleUpload.min.js
1 lines (1 loc) · 2.1 KB
/
simpleUpload.min.js
1
!function(a,b){var c=[],d=0;b.fn.simpleUpload=function(a){var e={url:null,change:null,error:null,types:null,fields:null,size:5120};return this.each(function(){function i(a){a.preventDefault(),f.simpleUploadSend()}var f=b(this),g=++d;f.data("simpleUpload",g),a=a||{},b.each(f.data(),function(b,c){"simpleUpload"!=b&&(a[b]="function"==typeof window[c]?window[c]:c)});var h=b.extend({},e,a);"function"==typeof h.change&&f.change(function(){h.change(f[0].files)}),h.trigger?h.trigger instanceof jQuery?h.trigger.click(i):b(document).on("click",h.trigger,i):f.change(i),c[g]=h})},b.fn.simpleUploadSend=function(a){return this.each(function(){var a=b(this),d=a.data("simpleUpload"),e=a.attr("name"),f=new FormData;params=c[d];for(var g in a[0].files)if("object"==typeof a[0].files[g]){var h=a[0].files[g];if(!b.typeCheck(h.name,params.types))return"function"==typeof params.error&¶ms.error({type:"fileType"}),a.val(""),!1;if(h.size/params.size>params.size)return"function"==typeof params.error&¶ms.error({type:"size"}),a.val(""),!1;f.append(e+"["+g+"]",h)}if("object"==typeof params.fields)for(g in params.fields)if(b.isArray(params.fields[g]))for(var i in params.fields[g])f.append(g+"["+i+"]",params.fields[g][i]);else f.append(g,params.fields[g]);var j={url:params.url,data:f,cache:!1,contentType:!1,processData:!1,type:"POST",complete:function(){a.val(""),params.complete&¶ms.complete(arguments)}};null!=params.error&&(j.error=params.error),params.success&&(j.success=params.success),params.async&&(j.async=params.async),params.beforeSend&&(j.beforeSend=params.beforeSend),params.global&&(j.global=params.global),params.dataType&&(j.dataType=params.dataType),params.contents&&(j.contents=params.contents),params.jsonp&&(j.jsonp=params.jsonp),params.jsonpCallback&&(j.jsonpCallback=params.jsonpCallback),params.password&&(j.password=params.password),params.username&&(j.username=params.username),params.statusCode&&(j.statusCode=params.statusCode),b.ajax(j)})},b.extend(!0,{typeCheck:function(a,c){return!b.isArray(c)||c.indexOf(b.getExt(a))>-1},getExt:function(a){return a.substr(a.lastIndexOf(".")+1).toLowerCase()}})}(window,window.jQuery);