JavaScript 上传文件(psd,压缩包等),图片,视频的实现方法
程序员文章站
2022-06-24 17:07:57
废话不多说了,直接给大家贴代码了,具体代码如下所示:
// 上传目标触发点
废话不多说了,直接给大家贴代码了,具体代码如下所示:
// 上传目标触发点 <input type="file" class="upvideo" name="upvideo" id="fileupload1" /> // 引入插件 <script type="text/javascript" src="{$img}/bstage/js/jquery.form.js" language="javascript"></script> /**方法调用**/ setfileupload({ name: 'fileupload1', backfun: upback1, beforefun: checkup1, loadfun:loadup, phpurl: '/ossphp/php/get.php?ft=video' }); // 上传前验证 function checkup1(n){ // 查看视频大小(mb) var fileobj = document.getelementbyid(n).files[0]; var relsize = parseint(fileobj.size/1024/1024); if(relsize > 10){ // 大于10mb alert('提示','视屏超过10mb,请重新上传!'); return false; } // 查看视频类型 var tv_id =document.getelementbyid(n).value;//根据id得到值 var index= tv_id.indexof("."); tv_id=tv_id.substring(index).tolowercase(); if(tv_id!=".rb"&&tv_id!=".rmvb"&&tv_id!=".mp4"&&tv_id!=".flv"){ alert("提示","不是指定视频格式,重新选择"); return false; } return true; } //上传中 function loadup(){ } // 上传成功后操作 function upback1(data){ // data 成功后的参数 } /**上传功能封装**/ function setfileupload(options){ var n = options.name; var phpurl = options.phpurl; var backfun = options.backfun; var beforefun = options.beforefun; var loadfun = options.loadfun; var accessid = ''; var accesskey = ''; var host = ''; var policybase64 = ''; var signature = ''; var callbackbody = ''; var filename = ''; var dirname = ''; var expire = 0; var upflag = true; if(phpurl==null || phpurl==''){console.log('phpurl is null');return false;} if(n==null || n==''){console.log('object is null');return false;} var backfun1 = $.isfunction(backfun) ? backfun : function(success, error){ console.log('no backfun'); }; var beforefun1 = $.isfunction(beforefun) ? beforefun : function(success, error){ return true; }; var loadfun1 = $.isfunction(loadfun) ? loadfun : function(success, error){ return true; }; var obj = $('#'+n); obj.change(function(){ if(beforefun1(n)){ if(n=="fileupload1"){ fileemb1(n); }else if(n == 'fileupload2'){ fileemb2(n); }else if(n == 'fileupload3'){ fileemb3(n); } } }); function fileemb1(n){ //oss add $("#"+n).attr('name','file'); var file1 = $("#"+n).val(); var filename = getfilename(file1); phpurl = phpurl + '&fname='+filename; get_signature(); var ldot = filename.lastindexof("."); var filetype = filename.substring(ldot + 1); $("#"+n).wrap("<form id='myupload1' action='"+host+"' method='post' enctype='multipart/form-data'></form>"); $("#myupload1").prepend("<input type='hidden' value='' name='name' id='name'></input><input type='hidden' value='"+dirname+"."+filetype+"' name='key' ></input><input type='hidden' value='"+policybase64+"' name='policy' id='policy'></input><input type='hidden' value='"+accessid+"' name='ossaccesskeyid' id='ossaccesskeyid'></input><input type='hidden' value='200' name='success_action_status' id='success_action_status' ></input><input type='hidden' value='"+callbackbody+"' name='callback' id='callback'></input><input type='hidden' value='"+signature+"' name='signature' id='signature'></input>"); //speed_width进度条类 //percent进度数字类 //load_content上传区域 $('#myupload1').ajaxsubmit({ datatype: 'json', //数据格式为json data:'', beforesend: function() { loadfun1(); if(!upflag)return false; upflag=false; }, uploadprogress: function(event, position, total, percentcomplete) { $('.upck_b').hide(); $('.videspeed').show(); percentcomplete = percentcomplete>=100?99:percentcomplete; var percentval = percentcomplete + '%'; //获得进度 $('.speed_num').css('width',percentval); $('.persent_em').html(percentcomplete); }, success: function(data) { //成功 $('.load_content').html('<em >图片上传</em>'); //上传成功后还原 $("#"+n).attr('name',''); setfileupload(options); upflag=true; backfun1(data); }, error:function(xhr){ //上传失败 $('.load_content').html('<em >图片上传</em>'); upflag=true; } }); } function getfilename(path){ var pos1 = path.lastindexof('/'); var pos2 = path.lastindexof('\\'); var pos = math.max(pos1, pos2) if( pos<0 ) return path; else return path.substring(pos+1); } now = timestamp = date.parse(new date()) / 1000; new_multipart_params = ''; function send_request(){ var xmlhttp = null; if (window.xmlhttprequest) { xmlhttp=new xmlhttprequest(); } else if (window.activexobject) { xmlhttp=new activexobject("microsoft.xmlhttp"); } if (xmlhttp!=null && phpurl) { //phpurl = '/ossphp/php/get.php?ft=resources'; xmlhttp.open( "get", phpurl, false ); xmlhttp.send( null ); return xmlhttp.responsetext; } else { alert("your browser does not support xmlhttp."); } }; function get_signature() { //可以判断当前expire是否超过了当前时间,如果超过了当前时间,就重新取一下.3s 做为缓冲 now = timestamp = date.parse(new date()) / 1000; //if (expire < now + 3) //{ body = send_request() var obj = eval ("(" + body + ")"); host = obj['host'] policybase64 = obj['policy'] accessid = obj['accessid'] signature = obj['signature'] expire = parseint(obj['expire']) callbackbody = obj['callback'] dirname = obj['dir'] return true; //} }; }
注:里面的细节还可以继续封装
以上所述是小编给大家介绍的javascript 上传文件(psd,压缩包等),图片,视频功能,希望对大家有所帮助