使用ajaxFileUpload上传文件流至服务器,同时提交多个参数
2016-08-25 09:03
537 查看
1.前端界面引入如下两个js文件,注意引入顺序,因为ajaxfileupload会用到jquery,所以jquery应该先引入
<scripttype="text/javascript"src="/js/jquery.min.js"></script> <scripttype="text/javascript"src="/js/open/ajaxfileupload.js"></script>
<tr> <td>申请单号:</td> <tdcolspan="7"><inputclass="common-textrequired"type="text" name="applyNo"id="applyNo"></input></td><!--提交文件流的时候,同时提交参数--> </tr> <tr> <td>合同文件:</td> <td><inputtype="file"id="filePath"name="filePath"></td>
<!--注意input的类型和name属性-->
</tr>
2.修改ajaxfileupload.js成如下
jQuery.extend({ createUploadIframe:function(id,uri) { //createframe varframeId='jUploadFrame'+id; if(window.ActiveXObject){ vario=document.createElement('<iframeid="'+frameId+'"name="'+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,data) { //createform varformId='jUploadForm'+id; varfileId='jUploadFile'+id; varform=$('<formaction=""method="POST"name="'+formId+'"id="'+formId+'"enctype="multipart/form-data"></form>'); varoldElement=$('#'+fileElementId); varnewElement=$(oldElement).clone(); $(oldElement).attr('id',fileId); $(oldElement).before(newElement); $(oldElement).appendTo(form); //增加文本参数的支持 if(data){ for(variindata){ $('<inputtype="hidden"name="'+i+'"value="'+data[i]+'"/>').appendTo(form); } } //setattributes $(form).css('position','absolute'); $(form).css('top','-1200px'); $(form).css('left','-1200px'); $(form).appendTo('body'); returnform; }, ajaxFileUpload:function(s){ //TODOintroduceglobalsettings,allowingtheclienttomodifythemforallrequests,notonlytimeout s=jQuery.extend({},jQuery.ajaxSettings,s); varid=newDate().getTime() varform=jQuery.createUploadForm(id,s.fileElementId,s.data); vario=jQuery.createUploadIframe(id,s.secureuri); varframeId='jUploadFrame'+id; varformId='jUploadForm'+id; //Watchforanewsetofrequests if(s.global&&!jQuery.active++) { jQuery.event.trigger("ajaxStart"); } varrequestDone=false; //Createtherequestobject varxml={} if(s.global) jQuery.event.trigger("ajaxSend",[xml,s]); //Waitforaresponsetocomeback varuploadCallback=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"; //Makesurethattherequestwassuccessfulornotmodified if(status!="error") { //processthedata(runsthexmlthroughhttpDataregardlessofcallback) vardata=jQuery.uploadHttpData(xml,s.dataType); //Ifalocalcallbackwasspecified,fireitandpassitthedata if(s.success) s.success(data,status); //Firetheglobalcallback if(s.global) jQuery.event.trigger("ajaxSuccess",[xml,s]); }else jQuery.handleError(s,xml,status); }catch(e) { status="error"; jQuery.handleError(s,xml,status,e); } //Therequestwascompleted if(s.global) jQuery.event.trigger("ajaxComplete",[xml,s]); //HandletheglobalAJAXcounter if(s.global&&!--jQuery.active) jQuery.event.trigger("ajaxStop"); //Processresult if(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 } } //Timeoutchecker if(s.timeout>0) { setTimeout(function(){ //Checktoseeiftherequestisstillhappening if(!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",evalitinglobalcontext if(type=="script") jQuery.globalEval(data); //GettheJavaScriptobject,ifJSONisused. if(type=="json") eval("data="+data); //evaluatescriptswithinhtml if(type=="html") jQuery("<div>").html(data).evalScripts(); //alert($('param',data).each(function(){alert($(this).attr('value'));})); returndata; } })3.js中调用ajaxFileUpload举例
$.ajaxFileUpload({ type:"post", url:"/test/lhpContractUpload",//后台接收url secureuri:false, fileElementId:'filePath',//file标签的id,name data:{ "applyNo":applyNo }, dataType:"json", success:function(data){ $("#lhpContractUpload").show().html(data); } });4.后台接收处理
publicStringlhpContractUpload(StringapplyNo,@RequestParam(value="filePath")MultipartFilefile){
try{
InputStreamio=file.getInputStream();//获取输入文件流
returnopenService.lhpContractUpload(applyNo,io);
}catch(IOExceptione){
e.printStackTrace();
}
return"上传失败";
}
相关文章推荐
- $ajaxFileUpload可以带参数文件上传源码及使用
- 浏览器图片\文件上传到服务器 ajaxfileupload.js使用实例
- [原创]使用ajaxFileUpload.js上传文件时附带额外参数。
- 使用ajaxfileupload同时上传多个文件
- ajaxfileupload带参数上传文件
- ASP.NET同时上传多个文件,和不使用HTMLFileInput上传的方法(resource about file upload)
- 使用ajaxFileUpload.js插件实现ajax无刷新上传文件
- 使用AjaxFileUpload.js实现文件异步上传
- Struts2中的异步提交(ajaxfileupload异步上传(图片)插件的使用)
- 解决网页异步上传图片时不断更换预览图片时数据库累积文件问题与ajaxUploadFile上传多个参数问题
- 使用ajaxfileupload.js实现ajax上传文件php版
- 使用ajaxFileUpload.js插件实现ajax无刷新上传文件
- ajaxfileupload带参数上传文件
- jquery 实现ajax 上传文件的功能(使用插件 ajaxfileupload)
- ajaxFileUpload 异步上传文件简单使用
- 使用AjaxFileUpload.js实现异步文件上传示例
- ajaxFileUpload.js 无刷新上传图片,支持多个参数同时上传,支持 ie6-ie10
- ajaxfileupload带参数上传文件
- ajaxFileUpload.js 无刷新上传图片,支持多个参数同时上传
- asp.net ajax AjaxFileUpload使用 多文件上传