ajaxFileUpload上传带参数,重临值改成json格式

插件--ajaxfileupload.jsjQuery.extend({createUploadIframe:function(id,uri){//createframevarframeId='jUploadFrame'+id;if(window.ActiveXObject){vario=document.createElement('iframename="'+frameId+'"/');if(typeofuri=='boolean'){io.src='javascript:false';}elseif(typeofuri=='string'){io.src=uri;}}else{vario=document.createElement('iframe');io.id=frameId;io.name=frameId;}io.style.position='absolute';io.style.top='-1000px';io.style.left='-1000px';document.body.appendChild(io);returnio},createUploadForm:function(id,fileElementId){//createformvarformId='jUploadForm'+id;varfileId='jUploadFile'+id;varform=$('formaction=""method="POST"name="'+formId+'"enctype="multipart/form-data"/form');varoldElement=$('#'+fileElementId);varnewElement=$(oldElement).clone();$(oldElement).attr('id',fileId);$(oldElement).before(newElement);$(oldElement).appendTo(form);//setattributes$(form).css('position','absolute');$(form).css('top','-1200px');$(form).css('left','-1200px');$(form).appendTo('body');returnform;},ajaxFileUpload:function(s){//TODOintroduceglobalsettings,allowingtheclienttomodifythemforallrequests,notonlytimeouts=jQuery.extend({},jQuery.ajaxSettings,s);varid=newDate().getTime()varform=jQuery.createUploadForm(id,s.fileElementId);vario=jQuery.createUploadIframe(id,s.secureuri);varframeId='jUploadFrame'+id;varformId='jUploadForm'+id;//Watchforanewsetofrequestsif(s.global!jQuery.active++){jQuery.event.trigger("ajaxStart");}varrequestDone=false;//Createtherequestobjectvarxml={}if(s.global)jQuery.event.trigger("ajaxSend",[xml,s]);//WaitforaresponsetocomebackvaruploadCallback=function(isTimeout){vario=document.getElementById(frameId);try{if(io.contentWindow){xml.responseText=io.contentWindow.document.body?io.contentWindow.document.body.innerHTML:null;xml.responseXML=io.contentWindow.document.XMLDocument?io.contentWindow.document.XMLDocument:io.contentWindow.document;}elseif(io.contentDocument){xml.responseText=io.contentDocument.document.body?io.contentDocument.document.body.innerHTML:null;xml.responseXML=io.contentDocument.document.XMLDocument?io.contentDocument.document.XMLDocument:io.contentDocument.document;}}catch(e){jQuery.handleError(s,xml,null,e);}if(xml||isTimeout=="timeout"){requestDone=true;varstatus;try{status=isTimeout!="timeout"?"success":"error";//Makesurethattherequestwassuccessfulornotmodifiedif(status!="error"){//processthedata(runsthexmlthrough)vardata=jQuery.uploadHttpData(xml,s.dataType);//Ifalocalcallbackwasspecified,fireitandpassitthedataif(s.success)s.success(data,status);//Firetheglobalcallbackif(s.global)jQuery.event.trigger("ajaxSuccess",[xml,s]);}elsejQuery.handleError(s,xml,status);}catch(e){status="error";jQuery.handleError(s,xml,status,e);}//Therequestwascompletedif(s.global)jQuery.event.trigger("ajaxComplete",[xml,s]);//HandletheglobalAJAXcounterif(s.global!--jQuery.active)jQuery.event.trigger("ajaxStop");//Processresultif(s.complete)s.complete(xml,status);jQuery(io).unbind()setTimeout(function(){try{$(io).remove();$(form).remove();}catch(e){jQuery.handleError(s,xml,null,e);}},100)xml=null}}//Timeoutcheckerif(s.timeout0){setTimeout(function(){//Checktoseeiftherequestisstillhappeningif(!requestDone)uploadCallback("timeout");},s.timeout);}try{//vario=$('#'+frameId);varform=$('#'+formId);$(form).attr('action',s.url);$(form).attr('method','POST');$(form).attr('target',frameId);if(form.encoding){form.encoding='multipart/form-data';}else{form.enctype='multipart/form-data';}$(form).submit();}catch(e){jQuery.handleError(s,xml,null,e);}if(window.attachEvent){document.getElementById(frameId).attachEvent('onload',uploadCallback);}else{document.getElementById(frameId).addEventListener('load',uploadCallback,false);}return{abort:function(){}};},uploadHttpData:function(r,type){vardata=!type;data=type=="xml"||data?r.responseXML:r.responseText;//Ifthetypeis"script",evalitinglobalcontextif(type=="script"卡塔尔国jQuery.globalEval(data卡塔尔;//GettheJavaScriptobject,ifJSONisused.if(type=="json"State of Qatareval("data="+data卡塔尔国;//evaluatescriptswithinhtmlif(type=="html"卡塔尔国jQuery("div"卡塔尔.html(dataState of Qatar.evalScripts(卡塔尔;//alert($('param',data卡塔尔.each(function(State of Qatar{alert($(this卡塔尔(قطر‎.attr('value')卡塔尔国;}State of Qatar卡塔尔国;returndata;}}卡塔尔---页面代码%@pagelanguage="java"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"""htmlheadmeta"Content-Type"content="text/html;charset=UTF-8"titleInserttitlehere/titlescripttype="text/javascript"src="jquery.js"/scriptscripttype="text/javascript"src="ajaxfileupload.js"/scriptscripttype="text/javascript"functionajaxFileUpload(){$("#loading"State of Qatar.ajaxStart(function(卡塔尔国{$(this卡塔尔(قطر‎.show(State of Qatar;}卡塔尔//早前上传文件时体现多少个图片.ajaxComplete(function(卡塔尔{$(this卡塔尔(قطر‎.hide(卡塔尔国;}卡塔尔;//文件上传完毕将图纸掩瞒起来$.ajaxFileUpload({url:'../gap/fileUpload.do',//用于文书上传的劳务器端诉求地址secureuri:false,//日常安装为falsefileElementId:'file',//文件上传空间的id属性inputtype="file"name="file"/dataType:'json',//再次回到值类型常常安装为jsonsuccess:function(data,status卡塔尔//服务器成功响应处理函数{alert(data.message卡塔尔(قطر‎;//从服务器重返的json中抽取message中的数据,个中message为在struts第22中学action中定义的分子变量if(typeof(data.error卡塔尔国!='undefined'){if(data.error!=''State of Qatar{alert(data.error卡塔尔;}else{alert(data.message卡塔尔国;}}},error:function(data,status,e卡塔尔国//服务器响应战败管理函数{alert(e卡塔尔国;}}State of Qatarreturnfalse;}/script/headbodyimgsrc="loading.gif"inputtype="file"name="file"/br/inputtype="button"value="上传"onclick="returnajaxFileUpload(State of Qatar;"/body/htmlaction类方法!@SuppressWarnings("deprecation"State of QatarpublicStringfileUpload(State of QatarthrowsException{Stringpath=ServletActionContext.getRequest(卡塔尔国.getRealPath("/upload1"卡塔尔国;try{Filef=this.getFile(卡塔尔;if(this.getFileFileName(卡塔尔国.endsWith(".exe"卡塔尔卡塔尔国{message="对不起,你上传的文件格式不容许!!!";returnERROR;}FileInputStreaminputStream=newFileInputStream(f);FileOutputStreamoutputStream=newFileOutputStream(path+"/"+this.getFileFileName());byte[]buf=newbyte[1024];intlength=0;while((length=inputStream.read(buf))!=-1卡塔尔(قطر‎{outputStream.write(buf,0,length卡塔尔(قطر‎;}inputStream.close(卡塔尔国;outputStream.flush(卡塔尔(قطر‎;}catch(Exceptione卡塔尔(قطر‎{e.printStackTrace(State of Qatar;message="对不起,文件上传战败了!!!!";}returnSUCCESS;}}

         }, 100)

if (window.ActiveXObject) {
var io = document.createElement('<iframe name="' + frameId + '" />');
if (typeof uri == 'boolean') {
io.src = 'javascript:false';
}
else if (typeof uri == 'string') {
io.src = uri;
}
}
else {
var io = document.createElement;
io.id = frameId;
io.name = frameId;
}
io.style.position = 'absolute';
io.style.top = '-1000px';
io.style.left = '-1000px';
document.body.appendChild;
return io
},
createUploadForm: function (id, fileElementId, data) {
//create form
var formId = 'jUploadForm' + id;
var fileId = 'jUploadFile' + id;
var form = $('<form action="" method="POST" name="' + formId + '" enctype="multipart/form-
data"></form>');
var oldElement = $('#' + fileElementId);
var newElement = $(oldElement).clone();
$(oldElement).attr('id', fileId);
$(oldElement).before(newElement);
$(oldElement).appendTo;
//<span style="color:#ff0000;">增添文本参数的支撑,更改本处,本处应有掌声 </span>
if {
for (var i in data) {
var temp = $('<input type="hidden" name="' + i + '" />');
temp.val;
temp.appendTo;
}
}
//set attributes
$.css('position', 'absolute');
$.css('top', '-1200px');
$.css('left', '-1200px');
$.appendTo;
return form;
},
ajaxFileUpload: function {
// TODO introduce global settings, allowing the client to modify them for all requests, not only timeout
s = jQuery.extend({}, jQuery.ajaxSettings, s);
var id = new Date().getTime()
var form = jQuery.createUploadForm(id, s.fileElementId, s.data);
var io = jQuery.createUploadIframe(id, s.secureuri);
var frameId = 'jUploadFrame' + id;
var formId = 'jUploadForm' + id;
// Watch for a new set of requests
if (s.global && !jQuery.active++) {
jQuery.event.trigger("ajaxStart");
}
var requestDone = false;
// Create the request object
var xml = {}
if
jQuery.event.trigger("ajaxSend", [xml, s]);
// Wait for a response to come back
var uploadCallback = function (isTimeout) {
var io = document.getElementById;
try {
if (io.contentWindow) {
xml.responseText = io.contentWindow.document.body ? io.contentWindow.document.body.innerHTML : null;
xml.responseXML = io.contentWindow.document.XMLDocument ? io.contentWindow.document.XMLDocument :
io.contentWindow.document;
} else if (io.contentDocument) {
xml.responseText = io.contentDocument.document.body ? io.contentDocument.document.body.innerHTML :
null;
xml.responseXML = io.contentDocument.document.XMLDocument ? io.contentDocument.document.XMLDocument
: io.contentDocument.document;
}
} catch {
jQuery.handleError(s, xml, null, e);
}
if (xml || isTimeout == "timeout") {
requestDone = true;
var status;
try {
status = isTimeout != "timeout" ? "success" : "error";
// Make sure that the request was successful or notmodified
if (status != "error") {
// process the data (runs the xml through httpData regardless of callback)
var data = jQuery.uploadHttpData(xml, s.dataType);
// If a local callback was specified, fire it and pass it the data
if (s.success)
s.success(data, status);
// Fire the global callback
if
jQuery.event.trigger("ajaxSuccess", [xml, s]);
} else
jQuery.handleError(s, xml, status);
} catch {
status = "error";
jQuery.handleError(s, xml, status, e);
}
// The request was completed
if
jQuery.event.trigger("ajaxComplete", [xml, s]);
// Handle the global AJAX counter
if (s.global && ! --jQuery.active)
jQuery.event.trigger("ajaxStop");
// Process result
if (s.complete)
s.complete(xml, status);
jQuery.unbind()
setTimeout(function () {
try {
$.remove();
$.remove();
} catch {
jQuery.handleError(s, xml, null, e);
}
}, 100)
xml = null
}
}
// Timeout checker
if (s.timeout > 0) {
setTimeout(function () {
// Check to see if the request is still happening
if (!requestDone) uploadCallback("timeout");
}, s.timeout);
}
try {
// var io = $('#' + frameId);
var form = $('#' + formId);
$.attr('action', s.url);
$.attr('method', 'POST');
$.attr('target', frameId);
if (form.encoding) {
form.encoding = 'multipart/form-data';
}
else {
form.enctype = 'multipart/form-data';
}
$.submit();
} catch {
jQuery.handleError(s, xml, null, e);
}
if (window.attachEvent) {
document.getElementById.attachEvent('onload', uploadCallback);
}
else {
document.getElementById.addEventListener('load', uploadCallback, false);
}
return { abort: function () { } };
},
uploadHttpData: function {
/*源代码
美高梅在线登录网址,var data = !type;
data = type == "xml" || data ? r.responseXML : r.responseText;
// If the type is "script", eval it in global context
if (type == "script")
jQuery.globalEval;
// Get the JavaScript object, if JSON is used.
if (type == "json")
eval("data = " + data);
// evaluate scripts within html
if (type == "html")
www.4688.com,jQuery("<div>").html.evalScripts();
//alert($('param', data).each(function(){alert.attr;}));
return data;
*/
//校正后归来格式给成json
var data = r.responseText;
var start = data.indexOf;
var end = data.indexOf;
var jsonStr = data.substring(start, end + 1);
return (jsonStr instanceof Object) ? jsonStr : eval("(" + jsonStr + ")");
}
})

        } catch(e)
  {   
            jQuery.handleError(s, xml, null, e);
        }
        if(window.attachEvent){
            document.getElementById(frameId).attachEvent('onload', uploadCallback);
        }
        else{
            document.getElementById(frameId).addEventListener('load', uploadCallback, false);
        }   
        return {abort: function () {}}; 

/*js上传代码*/

 

jQuery.extend({
createUploadIframe: function {
//create frame
var frameId = 'jUploadFrame' + id;

                setTimeout(function()
         { try
          {
           $(io).remove();
           $(form).remove(); 
           
          } catch(e)
          {
           jQuery.handleError(s, xml, null, e);
          }         

3.后台选用参数使用Request["后台选取的参数名"],这里值需求做json返类别化

            document.body.appendChild(io);

2.ajaxFileUpload回去值json格式,已在1.ajaxFileUpload.js文件修正其重临值,

                xml = null

/*个人心得

    createUploadIframe: function(id, uri)
 {
   //create frame
            var frameId = 'jUploadFrame' + id;
           
            if(window.ActiveXObject) {
                var io = document.createElement('<iframe id="' + frameId + '" name="' + frameId + '" />');
                if(typeof uri== 'boolean'){
                    io.src = 'javascript:false';
                }
                else if(typeof uri== 'string'){
                    io.src = uri;
                }
            }
            else {
                var io = document.createElement('iframe');
                io.id = frameId;
                io.name = frameId;
            }
            io.style.position = 'absolute';
            io.style.top = '-1000px';
            io.style.left = '-1000px';

*/