/** * 无刷新上传 * createname:雨中磐石 * homeurl:http://www.rockoa.com/ * Copyright (c) 2016 rainrock (xh829.com) * Date:2016-01-01 */ (function ($) { maxupgloble = 0; function rockupload(opts){ var me = this; var opts = js.apply({inputfile:'',initpdbool:false,initremove:true,uptype:'*',maxsize:5,onchange:function(){},onchangebefore:function(){},upurl:'',onprogress:function(){},urlparams:{},updir:'',onsuccess:function(){},quality:0.7,xu:0,fileallarr:[],autoup:true,oldids:'', onerror:function(){},fileidinput:'fileid', onabort:function(){}, allsuccess:function(){} },opts); this._init=function(){ for(var a in opts)this[a]=opts[a]; //加载最大可上传大小 if(maxupgloble==0)$.getJSON(js.apiurl('login','getmaxup'),function(res){ try{ if(res.code==200){ var maxup = parseFloat(res.data.maxup); me.maxsize= maxup; maxupgloble = maxup; }}catch(e){} }); if(maxupgloble>0)this.maxsize= maxupgloble; if(!this.autoup)return; if(this.initremove){ $('#'+this.inputfile+'').parent().remove(); var s='
'; $('body').append(s); } $('#'+this.inputfile+'').change(function(){ me.change(this, 0); }); }; this.reset=function(){ if(!this.autoup)return; var fids = 'form_'+this.inputfile+''; if(document[fids])document[fids].reset(); }; this.setparams=function(ars){ this.oparams = js.apply({uptype:this.uptype}, ars); this.uptype=this.oparams.uptype; }; this.setuptype=function(lx){ this.uptype = lx; }; this.setupurl=function(ul){ this.upurl = ul; }; this.click=function(ars){ if(this.upbool)return; this.setparams(ars); get(this.inputfile).click(); }; this.clear=function(){ this.fileallarr = []; this.filearr = {}; this.xu = 0; $('#'+this.fileview+'').html(''); }; this.changenext=function(max){ var nxd = this.allfilesnow+1; if(!max)max = 20; if(nxd>=this.allfileslen || nxd>=max){ if(nxd>1)js.msg('success','共有'+nxd+'个文件全部上传完成'); }else{ this.change(this.allfilesobj, nxd); } } this.change=function(o1,nxd){ if(!o1.files){ js.msg('msg','当前浏览器不支持上传1'); return; } if(!nxd)nxd=0; var f = o1.files[nxd]; this.allfilesobj = o1; this.allfileslen = o1.files.length; this.allfilesnow = nxd; if(!f || f.name=='/')return; var a = {filename:f.name,filesize:f.size,filesizecn:js.formatsize(f.size)}; if(a.filesize<=0){ js.msg('msg',''+f.name+'不存在'); return; } if(this.isfields(a))return; if(f.size>this.maxsize*1024*1024){ this.reset(); js.msg('msg','文件不能超过'+this.maxsize+'MB,当前文件'+a.filesizecn+''); return; } var nstr = this.onchangebefore(f); if(nstr){js.msg('msg',nstr);return;} var filename = f.name; var fileext = filename.substr(filename.lastIndexOf('.')+1).toLowerCase(); if(!this.uptype)this.uptype='*'; if(this.uptype=='image')this.uptype='jpg,gif,png,bmp,jpeg'; if(this.uptype=='word')this.uptype='doc,docx,pdf,xls,xlsx,ppt,pptx,txt'; if(this.uptype!='*'){ var upss=','+this.uptype+','; if(upss.indexOf(','+fileext+',')<0){ js.msg('msg','禁止文件类型,请选择'+this.uptype+''); return; } } a.fileext = fileext; a.isimg = js.isimg(fileext); if(a.isimg)a.imgviewurl = this.getimgview(o1); a.xu = this.xu; a.f = f; for(var i in this.oparams)a[i]=this.oparams[i]; this.filearr = a; var zc=this.fileallarr.push(a); //如果是图片压缩一下超过1M if(f.size>1024*1024 && a.isimg && this.quality<1){ this.compressimg(a.imgviewurl,f,function(nf){ a.filesize = nf.size; a.filesizecn = js.formatsize(nf.size); me.fileallarr[zc-1].f = nf; me.nnonchagn(a, nf, zc); }); }else{ this.nnonchagn(a, f, zc); } }; this.nnonchagn=function(a,f,zc){ this.xu++; this.onchange(a); this.reset(); if(!this.autoup){ var s=''; $('#'+this.fileview+'').append(s); $('#gm'+this.fileview+'_'+a.xu+'').click(function(){ me.s_gaiming(this); }); return; } this._startup(f); }; this.s_gaiming=function(o1){ var o,oi,one,fa; o = $(o1); oi = parseFloat($(o1).attr('oi')); fa = this.fileallarr[oi]; one= o.parent().find('font').html().replace('.'+fa.fileext+'',''); if(get('confirm_main')){ var nr = prompt('新文件名', one); if(nr){ var newfie = nr+'.'+fa.fileext; o.parent().find('font').html(newfie); me.fileallarr[oi].filename=newfie; } }else{ js.prompt('修改文件名','新文件名', function(jg,nr){ if(jg=='yes' && nr){ var newfie = nr+'.'+fa.fileext; o.parent().find('font').html(newfie); me.fileallarr[oi].filename=newfie; } }, one); } }; this.compressimg=function(path,fobj,call){ var img = new Image(); img.src = path; if(!call)call=function(){}; img.onload = function(){ var that = this; var w = that.width, h = that.height, scale = w / h; var quality = me.quality;//压缩图片质量 var canvas = document.createElement('canvas'); var ctx = canvas.getContext('2d'); var anw = document.createAttribute("width"); anw.nodeValue = w; var anh = document.createAttribute("height"); anh.nodeValue = h; canvas.setAttributeNode(anw); canvas.setAttributeNode(anh); ctx.drawImage(that, 0, 0, w, h); var base64 = canvas.toDataURL(fobj.type, quality); var nfobj = me.base64toblob(base64); call(nfobj); } }; this.base64toblob=function(urlData){ var arr = urlData.split(','), mime = arr[0].match(/:(.*?);/)[1], bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n); while(n--){ u8arr[n] = bstr.charCodeAt(n); } return new Blob([u8arr], {type:mime}); }; this.getimgview=function(o1){ try{ return URL.createObjectURL(o1.files.item(this.allfilesnow)); }catch(e){return false;} }; this.isfields=function(a){ var bo = false,i,d=this.fileallarr; for(i=0;i