ajax+php文件上传代码 这是一款精小精悍的文件上传代码,并且很实用的ajax php文件上传代码。 ajax文件上传原理很简单就是实时返回用户提交的数据,进行计算,这样只是局部刷新了页面,用户感觉不到的。 ajax+php教程文件上传代码 这是一款精小精悍的文件上传代码,并且很实用的ajax php文件上传代码。 ajax文件上传原理很简单就是实时返回用户提交的数据,进行计算,这样只是局部刷新了页面,用户感觉不到的。 */?> <br /> (function(){<br /> <br /> var d = document, w = window;<br /> <br /> <br /> function get(element){<br /> if (typeof element == "string")<br /> element =d.getelementbyid(element);<br /> return element;<br /> }<br /> <br /> function addevent(el, type, fn){<br /> if (w.addeventlistener){<br /> el.addeventlistener(type, fn, false);} else if (w.attachevent){var f = function(){<br /> fn.call(el, w.event);<br /> }; <br /> el.attachevent('on' + type, f)<br /> }}<br /> <br /> <br /> var toelement = function(){var div = d.createelement('div');<br /> return function(html){div.innerhtml = html;var el = div.childnodes[0];<br /> div.removechild(el);return el;}<br /> }();<br /> <br /> <br /> function hasclass(ele,cls){<br /> return ele.classname.match(new regexp('(\s|^)'+cls+'(\s|$)'));<br /> }<br /> <br /> function addclass(ele,cls) {<br /> if (!hasclass(ele,cls)) ele.classname += " "+cls;<br /> }<br /> <br /> function removeclass(ele,cls) {<br /> var reg = new regexp('(\s|^)'+cls+'(\s|$)');<br /> ele.classname=ele.classname.replace(reg,' ');<br /> }<br /> <br /> <br /> if (document.documentelement["getboundingclientrect"]){<br /> <br /> var getoffset = function(el){var box = el.getboundingclientrect(),<br /> doc = el.ownerdocument,body = doc.body,<br /> docelem = doc.documentelement,clienttop = docelem.clienttop || body.clienttop || 0,clientleft = docelem.clientleft || body.clientleft || 0,<br /> zoom = 1;<br /> if (body.getboundingclientrect) {var bound = body.getboundingclientrect();<br /> zoom = (bound.right - bound.left)/body.clientwidth;<br /> }<br /> <br /> if (zoom > 1){<br /> clienttop = 0;<br /> clientleft = 0;}<br /> var top = box.top/zoom + (window.pageyoffset || docelem && docelem.scrolltop/zoom || body.scrolltop/zoom) - clienttop,left = box.left/zoom + (window.pagexoffset|| docelem && docelem.scrollleft/zoom || body.scrollleft/zoom) - clientleft;<br /> <br /> return {<br /> top: top,left: left<br /> };}<br /> <br /> }else {<br /> var getoffset = function(el){<br /> if (w.jquery){<br /> return jquery(el).offset();<br /> }var top = 0, left = 0;<br /> do {top += el.offsettop || 0;<br /> left += el.offsetleft || 0;}<br /> while (el = el.offsetparent);return {left: left,<br /> top: top};}<br /> }<br /> <br /> <br /> <br /> <br /> <br /> function getbox(el){<br /> var left, right, top, bottom;var offset = getoffset(el);<br /> left=offset.left;<br /> top = offset.top;right = left + el.offsetwidth;bottom = top + el.offsetheight;return {<br /> left: left,<br /> right: right,top: top,bottom: bottom<br /> };<br /> }<br /> <br /> <br /> <br /> function getmousecoords(e){ <br /> <br /> if (!e.pagex && e.clientx){var zoom = 1; <br /> var body = document.body;<br /> <br /> if (body.getboundingclientrect) {<br /> var bound = body.getboundingclientrect();zoom = (bound.right - bound.left)/body.clientwidth;<br /> }<br /> <br /> <br /> return {<br /> x: e.clientx / zoom + d.body.scrollleft + d.documentelement.scrollleft, y: e.clienty / zoom + d.body.scrolltop + d.documentelement.scrolltop<br /> };<br /> }return {x: e.pagex,y: e.pagey<br /> };<br /> <br /> }<br /> <br /> <br /> <br /> <br /> <br /> var getuid = function(){var id = 0;return function(){<br /> return 'valumsajaxupload' + id++;<br /> }<br /> }();<br /> <br /> <br /> function filefrompath(file){return file.replace(/.*(/|\)/, "");<br /> }<br /> <br /> <br /> function getext(file){<br /> return (/[.]/.exec(file)) ? /[^.]+$/.exec(file.tolowercase()) : '';<br /> }<br /> <br /> <br /> <br /> <br /> <br /> <br /> var getxhr = function(){var xhr;<br /> return function(){if(xhr) return xhr;<br /> if (typeof xmlhttprequest !== 'undefined') {<br /> xhr = new xmlhttprequest();<br /> }else {var v = [<br /> "microsoft.xmlhttp","msxml2.xmlhttp.5.0",<br /> "msxml2.xmlhttp.4.0",<br /> "msxml2.xmlhttp.3.0",<br /> "msxml2.xmlhttp.2.0"];for (var i=0; i <v.length; i++){ try {xhr = new activexobject(v[i]);<br /> break;<br /> } catch (e){}}<br /> }<br /> return xhr;<br /> }<br /> }();<br /> <br /> <br /> <br /> <br /> <br /> <br /> // please use ajaxupload , ajax_upload will be removed in the next version<br /> ajax_upload = ajaxupload = function(button, options){<br /> <br /> if (button.jquery){button = button[0];} else if (typeof button == "string" && /^#.*/.test(button)){button = button.slice(1);}<br /> <br /> button = get(button);this._input = null;<br /> this._button = button;this._disabled = false;this._submitting = false;<br /> this._justclicked = false;<br /> this._parentdialog = d.body;<br /> if (window.jquery && jquery.ui && jquery.ui.dialog){var parentdialog = jquery(this._button).parents('.ui-dialog');if (parentdialog.length){this._parentdialog = parentdialog[0];}}<br /> this._settings = {action: 'upload.php',name: 'userfile',<br /> data: {},<br /> autosubmit: true,responsetype: false,closeconnection: '',<br /> hoverclass: 'hover',onchange: function(file, extension){},onsubmit: function(file, extension){},oncomplete: function(file, response) {}<br /> };<br /> <br /> <br /> for (var i in options) {this._settings[i] = options[i];}<br /> this._createinput();this._rerouteclicks();<br /> }<br /> <br /> <br /> <br /> <br /> // assigning methods to our class<br /> ajaxupload.prototype = {<br /> setdata : function(data){this._settings.data = data;},disable : function(){this._disabled = true;<br /> },enable : function(){this._disabled = false;},<br /> destroy : function(){<br /> if(this._input){<br /> if(this._input.parentnode){this._input.parentnode.removechild(this._input);<br /> }<br /> this._input = null;<br /> }<br /> },_createinput : function(){<br /> var self = this;<br /> var input = d.createelement("input");<br /> input.setattribute('type','file');input.setattribute('name', this._settings.name);<br /> <br /> var styles = {'position' : 'absolute'<br /> ,'margin': '-5px 0 0 -175px','padding': 0,'width': '220px','height': '30px','fontsize': '14px' ,'opacity': 0,'cursor': 'pointer'<br /> ,'display' : 'none','zindex' : 2147483583 ,'direction' : 'ltr'};<br /> <br /> <br /> for (var i in styles){<br /> input.style[i] = styles[i];}<br /> <br /> if ( ! (input.style.opacity === "0")){input.style.filter = "alpha(opacity=0)";}<br /> <br /> <br /> this._parentdialog.appendchild(input);<br /> addevent(input, 'change',function(){var file = filefrompath(this.value);if(self._settings.onchange.call(self, file, getext(file)) == false ){return;} <br /> if (self._settings.autosubmit){<br /> self.submit();}<br /> <br /> });<br /> <br /> <br /> addevent(input, 'click', function(){<br /> self.justclicked = true;settimeout(function(){self.justclicked = false;}, 2500);});this._input = input;<br /> },<br /> _rerouteclicks : function (){<br /> var self = this;<br /> <br /> <br /> var box, dialogoffset = {top:0, left:0}, over = false;<br /> addevent(self._button, 'mouseo教程ver', function(e){<br /> if (!self._input || over) return;<br /> over = true;box = getbox(self._button);<br /> <br /> <br /> <br /> if (self._parentdialog != d.body){<br /> dialogoffset = getoffset(self._parentdialog);}});<br /> addevent(document, 'mousemove', function(e){<br /> <br /> var input = self._input;<br /> if (!input || !over) return;<br /> if (self._disabled){<br /> removeclass(self._button, self._settings.hoverclass);input.style.display = 'none';return;<br /> }<br /> var c = getmousecoords(e);<br /> <br /> <br /> <br /> <br /> if ((c.x >= box.left) && (c.x <= box.right) && <br /> (c.y >= box.top) && (c.y <= box.bottom)){<br /> input.style.top = c.y - dialogoffset.top + 'px';<br /> input.style.left = c.x - dialogoffset.left + 'px';<br /> input.style.display = 'block';<br /> addclass(self._button,self._settings.hoverclass);<br /> } else {<br /> over = false;var check = setinterval(function(){<br /> if (self.justclicked){<br /> return;<br /> }<br /> <br /> if ( !over ){<br /> input.style.display = 'none';} clearinterval(check);<br /> }, 25);<br /> removeclass(self._button, self._settings.hoverclass);}});}, _createiframe : <br /> <br /> function(){// same value in safari :(<br /> var id = getuid();<br /> var iframe = toelement('<iframe src="网页特效:false;" name="' + id + '" />');iframe.id = id;iframe.style.display = 'none';<br /> d.body.appendchild(iframe);return iframe;},<br /> submit : function(){<br /> var self = this, settings = this._settings;<br /> if (this._input.value === ''){return;<br /> }<br /> <br /> var file = filefrompath(this._input.value); <br /> <br /> <br /> //1star<br /> if (!(settings.onsubmit.call(this, file, getext(file)) == false)) {var iframe = this._createiframe();<br /> var form = this._createform(iframe);<br /> form.appendchild(this._input);<br /> <br /> <br /> if (settings.closeconnection && /applewebkit|msie/.test(navigator.useragent)){ var xhr = getxhr();<br /> xhr.open('get', settings.closeconnection,false);<br /> xhr.send('');<br /> }<br /> <br /> form.submit();<br /> d.body.removechild(form);form = null;<br /> this._input = null;this._createinput();<br /> var todeleteflag = false;<br /> <br /> addevent(iframe, 'load',function(e){<br /> <br /> if (iframe.src == "javascript:'%3chtml%3e%3c/html%3e';" ||<br /> iframe.src == "javascript:'<html>