您好,登錄后才能下訂單哦!
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唯一標識 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); }); } // 驗證文件類型 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);
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。