File_upload_demo.html
<! Doctype html> simple_file_ Upload.js
(function ($) { var defaultsettings = { accepttypes:["JPG", "png"], // accepted upload file type changecallback:function (file) { } // custom input[ Type=file] change Event }; var renderHtml = " <input type= ' file ' style= ' Display:none; ' /> "; // add hidden function renderinputfile (target, Settings) { // Generate dialog unique identity 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");nbsp;// added to the document to 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 ("Incorrect file type!"); return; } changecallback (file); }); } // verify file Type 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);
Simple JS FileUpload control