您好,登錄后才能下訂單哦!
file_upload_demo.html
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script src="../static/dist/js/jquery-1.11.3.min.js"></script> <script src="../static/js/ys_ui_plugin/simple_file_upload.js"></script> </head> <body> <a href="javascript:void(0)">上傳文件</a> <img /> <script> $("a").simpleUploadFile({ changeCallback:function(file){ var fileReader = new FileReader(); fileReader.onload = function(){ $("img").attr("src",this.result); }; fileReader.readAsDataURL(file); } }); </script> </body> </html>
simple_file_upload.js
(function($){ var defaultSettings = { acceptTypes:["jpg","png"], // 接受的上傳文件類型 changeCallback:function(file){ } // 自定義input[type=file] change事件 }; var renderHtml = "<input type='file' style='display:none;'/>"; // 添加隱藏的 function renderInputFile(target,settings){ // 生成dialog唯一標(biāo)識(shí) var id = "ys_simple_file_upload_"+new Date().getTime()+""+parseInt(Math.random()); $(target).attr("ys_simple_file_upload",id); $(renderHtml).attr("id",id).appendTo("html"); // 添加到文檔中去 return $("#"+id); } function bindEventHandlers(container,settings){ var changeCallback = settings.changeCallback; $(container).change(function(e){ e.preventDefault(); e.stopPropagation(); var file = e.target.files[0]; if(!validateFileType(file,settings)){ alert("文件類型不正確!"); return; } changeCallback(file); }); } // 驗(yàn)證文件類型 function validateFileType(file,settings){ var acceptTypes = settings.acceptTypes; var name = file.name; var fileSuffix = name.substr(name.lastIndexOf(".")+1); for(var i=0;i<acceptTypes.length;i++){ var acceptType = acceptTypes[i]; if(acceptType==fileSuffix){ return true; } } return false; } var options = { simpleUploadFile:function(settings) { var mergedSettings = {}; $.extend(mergedSettings,defaultSettings,settings); var container = renderInputFile(this,mergedSettings); bindEventHandlers(container,mergedSettings); $(this).click(function(e){ e.preventDefault(); e.stopPropagation(); $(container).click(); }); } }; $.fn.extend(options); })(jQuery);
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。