<p> readfirst:</p><p> 1.upload files as normal way.Get the upload info from var $_FILES</p><p> 2.I use xajax as a lib ,so I don't hack any code but use the extend way,such as reload the class.</p><p> So if someone use this extend,he would not need to change his code a bit.</p><p> 3.Working well on (win2k,winXP.sp2)IE 6.0+sp1 / firefox 1.5x,2.x (my OS only have these)</p><p> 4.It is kind that let me konw there is any problem of this extend.</p><p> updates:</p><p> 2007-3-12 23:26:</p><p> 1.fix the FF endless loading problem(thx JensKlose 's tips).</p><p> 2.some tips for thoese who want to implement a upload progress meter , check out this link:</p><p> <a href="http://www.phpclasses.org/blog/post/61-File-upload-progress-meter-for-PHP-4-at-last.html">http://www.phpclasses.org/blog/post/61- … -last.html</a></p><p> There are only 3 files exists(actually,only 2 is necessary).</p><p> 1.xajaxExtend.php (server side script, save to the path as xajax.inc.php)</p><p> 2.xajax_js/xajax_extend.js (browser side script, save to the path as xajax.js)</p><p> 3.tests/uploadTest.php (demo(show you how it work), save to the tests path. Actually I modify this from tests/catchAllFunctionTest.php)</p><p> save to /xajax_0.2.4/xajaxExtend.php</p><p> Code:<code><?php<br />/**<br />* @author RainChen @ Mon Mar 12 23:25:46 CST 2007<br />* @uses xajax file upload extend<br />* @access public<br />* @version 0.1<br />*/<br />include_once('xajax.inc.php');<br />class xajaxExtend extends xajax<br />{<br /> <br /> function processRequests()<br /> {<br /> if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_GET['xajax']))<br /> {<br /> $this->initProcessRequests();<br /> }<br /> parent::processRequests();<br /> }<br /> <br /> function initProcessRequests()<br /> {<br /> $xajaxRequest = array();<br /> $xajaxRequest['xajaxr'] = @$_GET['xajaxr'];<br /> $xajaxRequest['xajax'] = @$_GET['xajax'];<br /> // reset RequestMode<br /> if(isset($_GET['xajax']))<br /> {<br /> $_GET['xajax'] = null;<br /> unset($_GET['xajax']);<br /> }<br /> // get the upload file local path<br /> foreach(array_keys($_FILES) as $name)<br /> {<br /> if(isset($_GET[$name]) && !isset($_POST[$name]))<br /> {<br /> $_POST[$name] = $this->_decodeUTF8Data($_GET[$name]);<br /> }<br /> }<br /> $xajaxargs = array($this->getOriginalData($_POST));<br /> $xajaxRequest['xajaxargs'] = $xajaxargs;<br /> $_POST = $xajaxRequest;<br /> }<br /> <br /> function getJavascript($sJsURI="", $sJsFile=NULL)<br /> { <br /> $html = parent::getJavascript($sJsURI,$sJsFile);<br /> // get the extend js<br /> if ($sJsFile == NULL) $sJsFile = "xajax_js/xajax_extend.js";<br /> if ($sJsURI != "" && substr($sJsURI, -1) != "/") $sJsURI .= "/";<br /> $html .= " <script type="text/javascript" src="" . $sJsURI . $sJsFile . ""></script><br />";<br /> return $html;<br /> }<br /> <br /> /**<br /> * get original request data from GET POST<br /> **/<br /> function getOriginalData($data)<br /> {<br /> if($data)<br /> {<br /> if(get_magic_quotes_gpc())<br /> {<br /> if (is_array($data))<br /> {<br /> foreach($data as $key=>$value)<br /> {<br /> $data[$key] = xajaxExtend::getOriginalData($value);<br /> }<br /> }<br /> else<br /> {<br /> $data = stripslashes($data);<br /> }<br /> }<br /> }<br /> return $data;<br /> }<br />}<br />?></code></p>
<p> </p>
<p> save to /xajax_0.2.4/xajax_js/xajax_extend.js</p><p> Code:<code>/**<br />* @author RainChen @ Mon Mar 12 22:56:02 CST 2007<br />* @uses xajax file upload extend<br />* @access public<br />* @version 0.1<br />*/<br />function xajax_extend()<br />{<br /> if(typeof(xajax) == 'undefined')<br /> {<br /> return false;<br /> }<br /> <br /> xajax.isIE = (document.all && navigator.appVersion.toLowerCase().indexOf('msie')!=-1) || false;<br /> xajax.newSessionID = function()<br /> {<br /> var sessionID;<br /> sessionID = new String(new Date().getTime());<br /> var random = new String(Math.random( )).substr(2);<br /> sessionID = sessionID + random;<br /> return sessionID;<br /> };<br /> <br /> // overlay this if you need to<br /> xajax.setStatusMessages = function(msg)<br /> {<br /> window.status = msg;<br /> };<br /> <br /> // overlay this if you need to<br /> xajax.setCursor = function(cursor)<br /> {<br /> if (document.body)<br /> document.body.style.cursor = cursor;<br /> };<br /> <br /> var loadingTimeout;<br /> // overlay this if you need to<br /> xajax.beforeUpload = function()<br /> {<br /> // init status<br /> if(xajaxWaitCursor) xajax.setCursor('wait');<br /> if(xajaxStatusMessages) xajax.setStatusMessages('Sending Request...');<br /> clearTimeout(loadingTimeout);<br /> loadingTimeout = setTimeout("xajax.loadingFunction();",400);<br /> if (xajaxDebug) xajax.DebugMessage("Starting xajax...");<br /> };<br /> xajax.upload = function(rpcFunc,formID,url)<br /> {<br /> var form = xajax.$(formID);<br /> if(!form)<br /> {<br /> return false;<br /> }<br /> if(form.encoding.toLowerCase() != 'multipart/form-data')<br /> {<br /> form.encoding = 'multipart/form-data';<br /> }<br /> if(form.method.toLowerCase() != 'post')<br /> {<br /> form.method = 'post';<br /> }<br /> var newSessionID = xajax.newSessionID();<br /> <br /> xajax.beforeUpload();<br /> if(!url)<br /> {<br /> url = xajaxRequestUri;<br /> }<br /> var separator = '?';<br /> if(url.indexOf('?') != -1)separator = '&';<br /> url += separator + 'xajax='+encodeURIComponent(rpcFunc);<br /> url += "&xajaxr=" + new Date().getTime();<br /> // get the upload file local path<br /> var formItem;<br /> var nodeName;<br /> for(var i=0; i<form.getElementsByTagName('input').length; i++)<br /> {<br /> formItem = form.elements[i];<br /> nodeName = new String(formItem.nodeName).toLowerCase();<br /> if(formItem.name == '' || nodeName == 'button')<br /> {<br /> continue;<br /> }<br /> if(formItem.type == 'file')<br /> {<br /> url += '&'+formItem.name+'='+encodeURIComponent(formItem.value);<br /> }<br /> }<br /> form.action = url;<br /> <br /> var iframeName = form.id + newSessionID;<br /> var iframe;<br /> if((iframe = xajax.$(iframeName)))<br /> {<br /> document.body.removeChild(iframe);<br /> }<br /> iframe = xajax.createIframe(iframeName,iframeName);<br /> form.target = iframeName;<br /> var xmlDoc;<br /> var responseXML;<br /> if(typeof iframe.onreadystatechange == 'object') // for IE<br /> {<br /> iframe.onreadystatechange = function()<br /> {<br /> if(iframe.readyState == 'complete' && !iframe.loaded)<br /> {<br /> // IE load twice (bug or feature?)<br /> iframe.loaded = true;<br /> xmlDoc = document.frames(iframe.id);<br /> if(xmlDoc.window.document.location != iframe.src)<br /> {<br /> responseXML = xmlDoc.window.document.XMLDocument;<br /> xajax.uploadResponse(responseXML,iframe, xmlDoc);<br /> }<br /> }<br /> }<br /> }<br /> else // for FF<br /> {<br /> iframe.onload = function()<br /> {<br /> xmlDoc = iframe.contentWindow;<br /> if(xmlDoc.window.document.location != iframe.src)<br /> {<br /> responseXML = xmlDoc.document;<br /> xajax.uploadResponse(responseXML,iframe, xmlDoc);<br /> }<br /> }<br /> }<br /> };<br /> <br /> xajax.uploadResponse = function(responseXML,iframeObj, xmlDoc)<br /> {<br /> // response the xml<br /> var error=false;<br /> if(responseXML)<br /> {<br /> try<br /> {<br /> xajax.processResponse(responseXML);<br /> }<br /> catch(e)<br /> {<br /> error = true;<br /> //xajax.DebugMessage(e.description);<br /> }<br /> }<br /> else<br /> {<br /> error = true;<br /> }<br /> <br /> if(error)<br /> {<br /> var responseText = new String(xmlDoc.document.body.innerHTML);<br /> xajax.uploadFalse(responseText);<br /> }<br /> else<br /> {<br /> xajax.uploadSuccess();<br /> }<br /> // remove the iframe after response<br /> setTimeout(function(){document.body.removeChild(iframeObj)}, 500);<br /> };<br /> <br /> // overlay this if you need to<br /> xajax.uploadFalse = function(responseText)<br /> {<br /> var errorString = "Error: the XML response that was returned from the server is invalid.";<br /> errorString += "<br />Received:<br />" + responseText;<br /> var trimmedResponseText = responseText.replace( /^s+/g, "" );// strip leading space<br /> trimmedResponseText = trimmedResponseText.replace( /s+$/g, "" );// strip trailing<br /> if (trimmedResponseText != responseText)<br /> errorString += "<br />You have whitespace in your response.";<br /> xajax.DebugMessage(errorString);<br /> xajax.setCursor('default');<br /> if (xajaxStatusMessages == true) xajax.setStatusMessages('Invalid XML response error');<br /> };<br /> <br /> // overlay this if you need to<br /> xajax.uploadSuccess = function()<br /> {<br /> xajax.setCursor('default');<br /> };<br /> <br /> xajax.createIframe = function(name,id)<br /> {<br /> var iframe;<br /> if(!id)<br /> {<br /> id = '';<br /> }<br /> if(xajax.isIE) // for IE<br /> {<br /> iframe = document.createElement('<iframe id="'+id+'" name="'+name+'">');<br /> }<br /> else // for FF<br /> {<br /> iframe = document.createElement('iframe');<br /> iframe.id = id;<br /> iframe.name = name;<br /> }<br /> iframe.width = '0';<br /> iframe.height = '0';<br /> iframe.style.display = 'none';<br /> iframe.scrolling = 'no';<br /> iframe.src = 'about:blank';<br /> document.body.appendChild(iframe);<br /> return iframe;<br /> };<br /> return true;<br />}<br />xajax_extend();</code></p>
<p> </p>
<p> save to /xajax_0.2.4/tests/uploadTest.php</p><p> Code:<code><?php<br />// set the defalut char encoding here if you don't not want to use UTF-8<br />//define("XAJAX_DEFAULT_CHAR_ENCODING" , 'gbk'); // set before the require file<br />error_reporting(0);<br />require_once("../xajaxExtend.php");<br />function testForm($formData)<br />{<br /> $objResponse = new xajaxResponse();<br /> $objResponse->addAlert("This is from the regular function");<br /> return test2ndFunction($formData, $objResponse);<br />}<br />function test2ndFunction($formData, $objResponse)<br />{<br /> $objResponse->addAlert("formData: " . print_r($formData, true));<br /> $objResponse->addAlert("$_FILES: " . print_r($_FILES, true));<br /> $objResponse->addAssign("submittedDiv", "innerHTML", nl2br(print_r($formData, true)));<br /> return $objResponse->getXML();<br />}<br />//$xajax = new xajax();<br />$xajax = new xajaxExtend;<br />$xajax->statusMessagesOn();<br />if(strtoupper(trim($xajax->sEncoding)) != 'UTF-8' )<br />{<br /> $xajax->decodeUTF8InputOn();<br />}<br />$xajax->registerFunction("testForm");<br />$xajax->processRequests();<br />header("Content-Type: text/html; charset=".$xajax->sEncoding);<br />?><br /><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"<br /> "http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-transitional.dtd"><br /><html xmlns="http://www.w3.org/1999/xhtml"><br /><head><br /><meta HTTP-EQUIV="content-type" CONTENT="text/html; charset=<?php print $xajax->sEncoding ?>"><br /><title>Catch-all Function Test | xajax Tests</title><br /><?php $xajax->printJavascript("../") ?><br /></head><br /><body><br /><h2><a href="index.php">xajax Tests</a></h2><br /><h1>file uploading Test</h1><br /><form id="testForm1" onsubmit="return false;"><br /><p><input type="text" id="textBox1" name="textBox1" value="This is some text" /></p><br /><p><input type="file" id="textBox2" name="somefile" /></p><br /><p><input type="file" id="textBox3" name="otherfile" /></p><br /><p><input type="submit" value="Submit Normal" onclick="xajax.call('testForm', [xajax.getFormValues('testForm1')]); return false;" /></p><br /></form><br /><hr /><br /><form id="testForm2" onsubmit="return xajax.upload('testForm', 'testForm2');" method="POST" enctype="multipart/form-data"><br /><p><input type="text" name="textBox1" value="This is some text2" /></p><br /><p><input type="file" name="somefile" /></p><br /><p><input type="file" name="otherfile" /></p><br /><p><input type="submit" value="upload" /></p><br /></form><br /><hr /><br /><form id="testForm3" ><br /><p><input type="text" name="textBox1" value="This is some text3" /></p><br /><p><input type="file" name="somefile" /></p><br /><p><input type="file" name="otherfile" /></p><br /><p><input type="submit" value="upload" /></p><br /></form><br /><script>xajax.$('testForm3').onsubmit = function(){xajax.upload('testForm', this.id)};</script><br /><hr /><br /><form id="testForm4" ><br /><p><input type="text" name="textBox1" value="This is some text4" /></p><br /><p><input type="file" name="somefile" /></p><br /><p><input type="file" name="otherfile" /></p><br /><p><input type="submit" value="upload" onclick="xajax.upload('testForm', 'testForm4')" /></p><br /></form><br /><hr /><br /><div id="submittedDiv"></div><br /></body><br /></html></code></p></p><p> then open the url http://xxxx/xajax_0.2.4/test/uploadTest.php in your browser for demo.</p><p> 来源:<a href="http://community.xajaxproject.org/viewtopic.php?id=1709">http://community.xajaxproject.org/viewtopic.php?id=1709</a></p></p>