原文链接:https://www.f2er.com/ajax/161973.html// JavaScript Document jQuery.extend({ createUploadIframe: function(id, uri) { //create frame var frameId = 'jUploadFrame' + id; if(window.ActiveXObject) //var io = document.createElement('<iframe id="' + frameId + '" name="' + frameId + '" />'); (jQuery.browser.version=="9.0" || jQuery"10.0"){ var io = document.createElement('iframe'); io.id = frameId; io.name ; }else "6.0" "7.0" "8.0"'<iframe id="' + frameId + '" name="' '" />'); (typeof uri== 'boolean'){ io.src 'javascript:false'; } 'string'= uri} else { ); io; io} io.style.position 'absolute'.top '-1000px'.left ; document.body.appendChild(io); return io}, createUploadForm fileElementId data//create form var formId 'jUploadForm' ; var fileId 'jUploadFile' var form = jQuery'<form action="" method="POST" name="' + formId '" id="' '" enctype="multipart/form-data"></form>'var oldElement '#' + fileElementId); var newElement (oldElement).clone(); jQuery).attr'id' fileId); jQuery).before(newElement).appendTo(form); //add data (data{ for var i in data{ $'<input type="hidden" name="' + i '" value="' + data[i] ); } } //set attributes jQuery).css'position' 'top''-1200px''left''body'return form ajaxFileUpload(s// TODO introduce global settings,allowing the client to modify them for all requests,not only timeout s ({}, jQuery.ajaxSettings svar id = s.id; //var id = s.fileElementId; .createUploadForm.fileElementIds.data.createUploadIframe.secureuri; ( s.global && ! jQuery.active++ ){ // Watch for a new set of requests jQueryevent.trigger( "ajaxStart" ); } var requestDone = false; // Create the request object var xml = {}; ){ jQuery"ajaxSend" [xml]); } var uploadCallback (isTimeout){ // Wait for a response to come back .getElementById(frameIdtry.contentWindow){ xml.responseText = io.document?io.innerHTML:null; xml.responseXML XMLDocument:io.contentDocumentcatch(e.handleError xml e( xml || isTimeout "timeout"){ requestDone truevar statustry { status = isTimeout != "timeout" ? "success" : "error"// Make sure that the request was successful or notmodified ( status "error" // process the data (runs the xml through httpData regardless of callback) var data .uploadHttpData( xml.dataType .success // ifa local callback was specified,fire it and pass it the data s.success( data status }; // Fire the global callback jQuery"ajaxSuccess"}; } else{ jQuery status} ){ status ; jQuery// The request was completed jQuery"ajaxComplete"}; // Handle the global AJAX counter ! --jQuery"ajaxStop"}; .complete){ s(xml}; jQuery).unbind(); setTimeout(){ ).remove(); }}, 100); xml }; // Timeout checker .timeout > 0 ){ setTimeout(){(!requestDone ){uploadCallback);}},0)">.timeout+ formId'action'.url'method''POST''target' frameId.encoding){ form.encoding 'multipart/form-data'{ form.enctype } jQuery).submit(); ){ jQuery/*if(window.attachEvent){ document.getElementById(frameId).attachEvent('onload',uploadCallback); } else{ document.getElementById(frameId).addEventListener('load',uploadCallback,false); } */ jQuery+ frameId).load(uploadCallbackreturn {abortfunction () {}}; uploadHttpData( r type !type; data = type "xml" || data ? r: r.responseText// ifthe type is "script",eval it in global context ( type "script" .globalEval( data } // Get the JavaScript object,ifJSON is used. "json" ){ data = r; var start = data.indexOf">"(start != -1var end "<" start + { data .substringendeval"data = " // evaluate scripts within html "html" "<div>").html).evalScripts(); } return data /*handleError: function( s,xml,status,e ) { // If a local callback was specified,fire it if ( s.error ) s.error( xml,e ); // Fire the global callback if ( s.global ) jQuery.event.trigger( "ajaxError",[xml,s,e] ); }*/ handleError xhr e // If a local callback was specified,fire it if .error { s.error.call.context || s// Fire the global callback { ? jQuery.context: jQuery).trigger"ajaxError"[xhr} });