前提条件: ajaxFileUpload.js插件多文件上传 步骤: 1、修改源码,(源码只支持单个文件的上传): 复制代码 代码如下: //修改前代码------- //var oldElement = jQuery('#' + fileElementId); //var newElement = jQuery(oldElement).clone(); //jQuery(oldElement).attr('id', fileId); //jQuery(oldElement).before(newElement); //jQuery(oldElement).appendTo(form); //修改前代码------- //修改后代码------- for(var i in fileElementId){ var oldElement = jQuery('#' + fileElementId[i]); var newElement = jQuery(oldElement).clone(); jQuery(oldElement).attr('id', fileId); jQuery(oldElement).before(newElement); jQuery(oldElement).appendTo(form); } //修改后代码------- 2、使用方法: 复制代码 代码如下: $.ajaxFileUpload({ url : "./upload/upload.action", secureuri : false, //fileElementId:'uploadfile1',//原使用方法 fileElementId : ['uploadfile1','uploadfile2','uploadfile3','uploadfile4','uploadfile5'],//现使用方法 dataType : 'json', success : function(data) { ajaxLoadEnd(); if (data.result == "success") { $.messager.alert('信息','导入成功。','info'); } else { $('#import_right').dialog('open'); $.messager.alert('信息','导入失败。<br>错误信息:'+data.message,'error'); } }, error : function(data, status, e) { ajaxLoadEnd(); $.messager.alert('信息','导入失败。<br>错误信息:网络异常或表单数据错误。','error'); } });

1 jQuery.extend({ 2 3 4 createUploadIframe: function (id, uri) { 5 //create frame 6 var frameId = 'jUploadFrame' + id; 7 var iframeHtml = '<iframe id="' + frameId + '" name="' + frameId + '" style="position:absolute; top:-9999px; left:-9999px"'; 8 if (window.ActiveXObject) { 9 if (typeof uri == 'boolean') { 10 iframeHtml += ' src="' + 'javascript:false' + '"'; 11 12 } 13 else if (typeof uri == 'string') { 14 iframeHtml += ' src="' + uri + '"'; 15 16 } 17 } 18 iframeHtml += ' />'; 19 jQuery(iframeHtml).appendTo(document.body); 20 21 return jQuery('#' + frameId).get(0); 22 }, 23 createUploadForm: function (id, fileElementId) { 24 //create form 25 var formId = 'jUploadForm' + id; 26 var fileId = 'jUploadFile' + id; 27 var form = jQuery('<form action="" method="POST" name="' + formId + '" id="' + formId + '" enctype="multipart/form-data"></form>'); 28 29 30 //var oldElement = jQuery('#' + fileElementId); 31 //var newElement = jQuery(oldElement).clone(); 32 //jQuery(oldElement).attr('id', fileId); 33 //jQuery(oldElement).before(newElement); 34 //jQuery(oldElement).appendTo(form); 35 36 for (var i in fileElementId) { 37 var oldElement = jQuery('#' + fileElementId[i]); 38 var newElement = jQuery(oldElement).clone(); 39 jQuery(oldElement).attr('id', fileId); 40 jQuery(oldElement).before(newElement); 41 jQuery(oldElement).appendTo(form); 42 } 43 44 45 46 //set attributes 47 jQuery(form).css('position', 'absolute'); 48 jQuery(form).css('top', '-1200px'); 49 jQuery(form).css('left', '-1200px'); 50 jQuery(form).appendTo('body'); 51 return form; 52 }, 53 54 ajaxFileUpload: function (s) { 55 // TODO introduce global settings, allowing the client to modify them for all requests, not only timeout 56 s = jQuery.extend({}, jQuery.ajaxSettings, s); 57 var id = new Date().getTime() 58 var form = jQuery.createUploadForm(id, s.fileElementId); 59 var io = jQuery.createUploadIframe(id, s.secureuri); 60 var frameId = 'jUploadFrame' + id; 61 var formId = 'jUploadForm' + id; 62 // Watch for a new set of requests 63 if (s.global && !jQuery.active++) { 64 jQuery.event.trigger("ajaxStart"); 65 } 66 var requestDone = false; 67 // Create the request object 68 var xml = {} 69 if (s.global) 70 jQuery.event.trigger("ajaxSend", [xml, s]); 71 // Wait for a response to come back 72 var uploadCallback = function (isTimeout) { 73 var io = document.getElementById(frameId); 74 try { 75 if (io.contentWindow) { 76 xml.responseText = io.contentWindow.document.body ? io.contentWindow.document.body.innerHTML : null; 77 xml.responseXML = io.contentWindow.document.XMLDocument ? io.contentWindow.document.XMLDocument : io.contentWindow.document; 78 79 } else if (io.contentDocument) { 80 xml.responseText = io.contentDocument.document.body ? io.contentDocument.document.body.innerHTML : null; 81 xml.responseXML = io.contentDocument.document.XMLDocument ? io.contentDocument.document.XMLDocument : io.contentDocument.document; 82 } 83 } catch (e) { 84 jQuery.handleError(s, xml, null, e); 85 } 86 if (xml || isTimeout == "timeout") { 87 requestDone = true; 88 var status; 89 try { 90 status = isTimeout != "timeout" ? "success" : "error"; 91 // Make sure that the request was successful or notmodified 92 if (status != "error") { 93 // process the data (runs the xml through httpData regardless of callback) 94 var data = jQuery.uploadHttpData(xml, s.dataType); 95 // If a local callback was specified, fire it and pass it the data 96 if (s.success) 97 s.success(data, status); 98 99 // Fire the global callback 100 if (s.global) 101 jQuery.event.trigger("ajaxSuccess", [xml, s]); 102 } else 103 jQuery.handleError(s, xml, status); 104 } catch (e) { 105 status = "error"; 106 jQuery.handleError(s, xml, status, e); 107 } 108 109 // The request was completed 110 if (s.global) 111 jQuery.event.trigger("ajaxComplete", [xml, s]); 112 113 // Handle the global AJAX counter 114 if (s.global && ! --jQuery.active) 115 jQuery.event.trigger("ajaxStop"); 116 117 // Process result 118 if (s.complete) 119 s.complete(xml, status); 120 121 jQuery(io).unbind() 122 123 setTimeout(function () { 124 try { 125 jQuery(io).remove(); 126 jQuery(form).remove(); 127 128 } catch (e) { 129 jQuery.handleError(s, xml, null, e); 130 } 131 132 }, 100) 133 134 xml = null 135 136 } 137 } 138 // Timeout checker 139 if (s.timeout > 0) { 140 setTimeout(function () { 141 // Check to see if the request is still happening 142 if (!requestDone) uploadCallback("timeout"); 143 }, s.timeout); 144 } 145 try { 146 147 var form = jQuery('#' + formId); 148 jQuery(form).attr('action', s.url); 149 jQuery(form).attr('method', 'POST'); 150 jQuery(form).attr('target', frameId); 151 if (form.encoding) { 152 jQuery(form).attr('encoding', 'multipart/form-data'); 153 } 154 else { 155 jQuery(form).attr('enctype', 'multipart/form-data'); 156 } 157 jQuery(form).submit(); 158 159 } catch (e) { 160 jQuery.handleError(s, xml, null, e); 161 } 162 163 jQuery('#' + frameId).load(uploadCallback); 164 return { abort: function () { } }; 165 166 }, 167 168 uploadHttpData: function (r, type) { 169 var data = !type; 170 data = type == "xml" || data ? r.responseXML : r.responseText; 171 // If the type is "script", eval it in global context 172 if (type == "script") 173 jQuery.globalEval(data); 174 // Get the JavaScript object, if JSON is used. 175 if (type == "json") 176 eval("data = " + data); 177 // evaluate scripts within html 178 if (type == "html") 179 jQuery("<div>").html(data).evalScripts(); 180 181 return data; 182 }, 183 184 185 handleError: function( s, xhr, status, e ) { 186 // If a local callback was specified, fire it 187 if ( s.error ) { 188 s.error.call( s.context || s, xhr, status, e ); 189 } 190 191 // Fire the global callback 192 if ( s.global ) { 193 (s.context ? jQuery(s.context) : jQuery.event).trigger( "ajaxError", [xhr, s, e] ); 194 } 195 } 196 })