這篇文章給大家分享的是有關(guān)JavaScript如何實現(xiàn)上傳文件,圖片,視頻功能的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
江西ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書合作)期待與您的合作!
具體代碼如下所示:
// 上傳目標(biāo)觸發(fā)點 // 引入插件 /**方法調(diào)用**/ 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;//根據(jù)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 成功后的參數(shù) } /**上傳功能封裝**/ 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(""); $("#myupload1").prepend(""); //speed_width進(jìn)度條類 //percent進(jìn)度數(shù)字類 //load_content上傳區(qū)域 $('#myupload1').ajaxSubmit({ dataType: 'json', //數(shù)據(jù)格式為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 + '%'; //獲得進(jìn)度 $('.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; } }); } 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() { //可以判斷當(dāng)前expire是否超過了當(dāng)前時間,如果超過了當(dāng)前時間,就重新取一下.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; //} }; }
感謝各位的閱讀!關(guān)于“JavaScript如何實現(xiàn)上傳文件,圖片,視頻功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!