JavaScript 上传文件(psd,压缩包等),图片,视频的实现方法
废话不多说了,直接给大家贴代码了,具体代码如下所示:
//上传目标触发点//引入插件 /**方法调用**/ setFileUpload({ name:'fileupload1', backfun:upback1, beforefun:checkUp1, loadfun:loadup, phpUrl:'/ossphp/php/get.php?ft=video' }); //上传前验证 functioncheckUp1(n){ //查看视频大小(mb) varfileObj=document.getElementById(n).files[0]; varrelSize=parseInt(fileObj.size/1024/1024); if(relSize>10){//大于10mb alert('提示','视屏超过10MB,请重新上传!'); returnfalse; } //查看视频类型 vartv_id=document.getElementById(n).value;//根据id得到值 varindex=tv_id.indexOf("."); tv_id=tv_id.substring(index).toLowerCase(); if(tv_id!=".rb"&&tv_id!=".rmvb"&&tv_id!=".mp4"&&tv_id!=".flv"){ alert("提示","不是指定视频格式,重新选择"); returnfalse; } returntrue; } //上传中 functionloadup(){ } //上传成功后操作 functionupback1(data){ //data成功后的参数 } /**上传功能封装**/ functionsetFileUpload(options){ varn=options.name; varphpUrl=options.phpUrl; varbackfun=options.backfun; varbeforefun=options.beforefun; varloadfun=options.loadfun; varaccessid=''; varaccesskey=''; varhost=''; varpolicyBase64=''; varsignature=''; varcallbackbody=''; varfilename=''; vardirname=''; varexpire=0; varupflag=true; if(phpUrl==null||phpUrl==''){console.log('phpurlisnull');returnfalse;} if(n==null||n==''){console.log('objectisnull');returnfalse;} varbackfun1=$.isFunction(backfun)? backfun: function(success,error){ console.log('nobackfun'); }; varbeforefun1=$.isFunction(beforefun)? beforefun: function(success,error){ returntrue; }; varloadfun1=$.isFunction(loadfun)? loadfun: function(success,error){ returntrue; }; varobj=$('#'+n); obj.change(function(){ if(beforefun1(n)){ if(n=="fileupload1"){ fileEmb1(n); }elseif(n=='fileupload2'){ fileEmb2(n); }elseif(n=='fileupload3'){ fileEmb3(n); } } }); functionfileEmb1(n){ //ossadd $("#"+n).attr('name','file'); varfile1=$("#"+n).val(); varfileName=getFileName(file1); phpUrl=phpUrl+'&fname='+fileName; get_signature(); varldot=fileName.lastIndexOf("."); varfiletype=fileName.substring(ldot+1); $("#"+n).wrap(" "); $("#myupload1").prepend(" "); //speed_width进度条类 //percent进度数字类 //load_content上传区域 $('#myupload1').ajaxSubmit({ dataType:'json',//数据格式为json data:'', beforeSend:function(){ loadfun1(); if(!upflag)returnfalse; upflag=false; }, uploadProgress:function(event,position,total,percentComplete){ $('.upck_b').hide(); $('.videspeed').show(); percentComplete=percentComplete>=100?99:percentComplete; varpercentVal=percentComplete+'%';//获得进度 $('.speed_num').css('width',percentVal); $('.persent_em').html(percentComplete); }, success:function(data){//成功 $('.load_content').html('图片上传'); //上传成功后还原 $("#"+n).attr('name',''); setFileUpload(options); upflag=true; backfun1(data); }, error:function(xhr){//上传失败 $('.load_content').html('图片上传'); upflag=true; } }); } functiongetFileName(path){ varpos1=path.lastIndexOf('/'); varpos2=path.lastIndexOf('\\'); varpos=Math.max(pos1,pos2) if(pos<0) returnpath; else returnpath.substring(pos+1); } now=timestamp=Date.parse(newDate())/1000; new_multipart_params=''; functionsend_request(){ varxmlhttp=null; if(window.XMLHttpRequest) { xmlhttp=newXMLHttpRequest(); } elseif(window.ActiveXObject) { xmlhttp=newActiveXObject("Microsoft.XMLHTTP"); } if(xmlhttp!=null&&phpUrl) { //phpUrl='/ossphp/php/get.php?ft=resources'; xmlhttp.open("GET",phpUrl,false); xmlhttp.send(null); returnxmlhttp.responseText; } else { alert("YourbrowserdoesnotsupportXMLHTTP."); } }; functionget_signature() { //可以判断当前expire是否超过了当前时间,如果超过了当前时间,就重新取一下.3s做为缓冲 now=timestamp=Date.parse(newDate())/1000; //if(expire 注:里面的细节还可以继续封装
以上所述是小编给大家介绍的JavaScript上传文件(psd,压缩包等),图片,视频功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!