我在做一个多文件上传,整体用的EXT,所以想用EXT的异步请求完成上传,上传没问题,就是上传完了,不执行回调函数,老自动弹一个下载"uploadFile.action"文件,其实就是一个json,好像是根本没执行回调函数,求各位老鸟解释下,并帮我解决,我不想别人上传完,总提示下载"uploadFile.action"。
页面代码:
[code="java"]
上传附件 | |||
添加附件: | |||
|
|||
温馨提示:最多可同时上传 个文件,只允许上传 文件。 | |||
<br> //以下为附件js代码<br> var isIE = (document.all) ? true : false;</p> <p>var $ = function (id) {<br> return "string" == typeof id ? document.getElementById(id) : id;<br> };</p> <p>var Class = {<br> create: function() {<br> return function() {<br> this.initialize.apply(this, arguments);<br> }<br> }<br> }</p> <p>var Extend = function(destination, source) {<br> for (var property in source) {<br> destination[property] = source[property];<br> }<br> }</p> <p>var Bind = function(object, fun) {<br> return function() {<br> return fun.apply(object, arguments);<br> }<br> }</p> <p>var Each = function(list, fun){<br> for (var i = 0, len = list.length; i < len; i++) { fun(list[i], i); }<br> };</p> <p>//提交操作<br> function doUpload(){<br> if(Efs.getExt("uploadForm").isValid()){<br> myMask.show();<br> Ext.Ajax.request({<br><br> //请求地址<br><br> url: 'uploadFile.action',<br><br> //提交参数组<br><br> fileUpload: true,<br> form: uploadForm,<br> scope: uploadForm,<br><br> //成功时回调<br><br> success: function(response, options) {<br><br> //获取响应的json字符串<br><br> myMask.hide();<br> var json = response.responseText; <br> var o= Ext.util.JSON.decode(json);<br><br> // var s="<s:text name="o.msg"/>";<br><br> var s=o.msg;<br><br> Ext.Msg.alert('<s:text name="msg_title"/>', s);<br><br> },<br> failure: function (response,options){<br> myMask.hide();<br> var st='<s:text name="'+o.msg+'"/>';<br><br> Ext.Msg.alert('<s:text name="msg_title"/>', st); <br> }<br><br> }); <br> }<br> else<br> Ext.Msg.alert('<s:text name="msg_title"/>', '<s:text name="msg_mustFill"/>');<br> }</p> <p>//文件上传<br> var FileUpload = Class.create();<br> FileUpload.prototype = {<br> //表单对象,文件控件存放空间<br> initialize: function(form, folder, options) {</p> <pre><code>this.Form = $(form);//表单 this.Folder = $(folder);//文件控件存放空间 this.Files = [];//文件集合 this.SetOptions(options); this.FileName = this.options.FileName; this._FrameName = this.options.FrameName; this.Limit = this.options.Limit; this.Distinct = !!this.options.Distinct; this.ExtIn = this.options.ExtIn; this.ExtOut = this.options.ExtOut; this.onIniFile = this.options.onIniFile; this.onEmpty = this.options.onEmpty; this.onNotExtIn = this.options.onNotExtIn; this.onExtOut = this.options.onExtOut; this.onLimite = this.options.onLimite; this.onSame = this.options.onSame; this.onFail = this.options.onFail; this.onIni = this.options.onIni; if(!this._FrameName){ //为每个实例创建不同的iframe this._FrameName = "uploadFrame_" + Math.floor(Math.random() * 1000); //ie不能修改iframe的name var oFrame = isIE ? document.createElement("<iframe name=\"" + this._FrameName + "\">") : document.createElement("iframe"); //为ff设置name oFrame.name = this._FrameName; oFrame.style.display = "none"; //在ie文档未加载完用appendChild会报错 document.body.insertBefore(oFrame, document.body.childNodes[0]); } //设置form属性,关键是target要指向iframe this.Form.target = this._FrameName; this.Form.method = "post"; //注意ie的form没有enctype属性,要用encoding this.Form.encoding = "multipart/form-data"; //整理一次 this.Ini(); </code></pre> <p>},<br> //设置默认属性<br> SetOptions: function(options) {<br> this.options = {//默认值<br> FileName: "files",//文件上传控件的name,配合后台使用<br> FrameName: "",//iframe的name,要自定义iframe的话这里设置name<br> onIniFile: function(){},//整理文件时执行(其中参数是file对象)<br> onEmpty: function(){},//文件空值时执行<br> Limit: 0,//文件数限制,0为不限制<br> onLimite: function(){},//超过文件数限制时执行<br> Distinct: true,//是否不允许相同文件<br> onSame: function(){},//有相同文件时执行<br> ExtIn: [],//允许后缀名<br> onNotExtIn: function(){},//不是允许后缀名时执行<br> ExtOut: [],//禁止后缀名,当设置了ExtIn则ExtOut无效<br> onExtOut: function(){},//是禁止后缀名时执行<br> onFail: function(){},//文件不通过检测时执行(其中参数是file对象)<br> onIni: function(){}//重置时执行<br> };<br> Extend(this.options, options || {});<br> },<br> //整理空间<br> Ini: function() {<br> //整理文件集合<br> this.Files = [];<br> //整理文件空间,把有值的file放入文件集合<br> Each(this.Folder.getElementsByTagName("input"), Bind(this, function(o){<br> if(o.type == "file"){ o.value && this.Files.push(o); this.onIniFile(o); }<br> }))<br> //插入一个新的file<br> var file = document.createElement("input");<br> file.name = this.FileName; file.type = "file"; file.onchange = Bind(this, function(){ this.Check(file); this.Ini(); });<br> this.Folder.appendChild(file);<br> //执行附加程序<br> this.onIni();<br> //添加附件成功,显示列表<br> if(this.Files.length>0){<br> document.getElementById('fileList').style.display="";<br> document.getElementById('uploadButton').style.display="";<br> }<br> },<br> //检测file对象<br> Check: function(file) {<br> //检测变量<br> var bCheck = true;<br> //空值、文件数限制、后缀名、相同文件检测<br> if(!file.value){<br> bCheck = false; this.onEmpty();<br> } else if(this.Limit && this.Files.length >= this.Limit){<br> bCheck = false; this.onLimite();<br> } else if(!!this.ExtIn.length && !RegExp(".(" + this.ExtIn.join("|") + ")$", "i").test(file.value)){<br> //检测是否允许后缀名<br> bCheck = false; this.onNotExtIn();<br> } else if(!!this.ExtOut.length && RegExp(".(" + this.ExtOut.join("|") + ")$", "i").test(file.value)) {<br> //检测是否禁止后缀名<br> bCheck = false; this.onExtOut();<br> } else if(!!this.Distinct) {<br> Each(this.Files, function(o){ if(o.value == file.value){ bCheck = false; } })<br> if(!bCheck){ this.onSame(); }<br> }<br> //没有通过检测<br> !bCheck && this.onFail(file);<br> },<br> //删除指定file<br> Delete: function(file) {<br> //移除指定file<br> this.Folder.removeChild(file); this.Ini();<br> if(this.Folder.getElementsByTagName("input").length==1){//没有附件时隐藏<br> document.getElementById('fileList').style.display="none";<br> document.getElementById('uploadButton').style.display="none";<br> }<br> },<br> //删除全部file<br> Clear: function() {<br> //清空文件空间<br> Each(this.Files, Bind(this, function(o){ this.Folder.removeChild(o); })); this.Ini();<br> document.getElementById('fileList').style.display="none";//清空同样隐藏<br> document.getElementById('uploadButton').style.display="none";<br> }<br> }</p> <p>var fu = new FileUpload("uploadForm", "idFile", { Limit: 3, ExtIn: [],<br> onIniFile: function(file){ file.value ? file.style.display = "none" : this.Folder.removeChild(file); },<br> onEmpty: function(){ alert("请选择一个文件"); },<br> onLimite: function(){ alert("超过上传限制"); },<br> onSame: function(){ alert("已经有相同文件"); },<br> // ExtIn: ["jpg", "gif"] 可以限制上传文件后缀名,不写即为所有<br> // onNotExtIn: function(){ alert("只允许上传" + this.ExtIn.join(",") + "文件"); },<br> onFail: function(file){ this.Folder.removeChild(file); },<br> onIni: function(){<br> //显示文件列表<br> var arrRows = [];<br> if(this.Files.length){<br> var oThis = this;<br> Each(this.Files, function(o){<br> var a = document.createElement("a"); a.innerHTML = "取消"; a.href = "javascript:void(0);";<br> a.onclick = function(){ oThis.Delete(o); return false; };<br> arrRows.push([o.value, a]);<br> });<br> } else { <br> arrRows.push(["<font color='gray'>没有添加文件</font>", " "]); <br> }<br> AddList(arrRows);</p> <pre><code> //设置按钮 //$("idBtnupload").disabled = $("idBtndel").disabled = this.Files.length <= 0; } </code></pre> <p>});</p> <p>$("idBtnupload").onclick = function(){<br> //显示文件列表<br> var arrRows = [];<br> Each(fu.Files, function(o){ arrRows.push([o.value, " "]); });<br> AddList(arrRows);</p> <pre><code>fu.Folder.style.display = "none"; $("idProcess").style.display = ""; $("idMsg").innerHTML = "正在添加文件到您的网盘中,请稍候……<br />有可能因为网络问题,出现程序长时间无响应,请点击“<a href='?'><font color='red'>取消</font></a>”重新上传文件"; doUpload(); $("idProcess").style.display = "none"; //fu.Form.submit(); </code></pre> <p>}</p> <p>//用来添加文件列表的函数<br> function AddList(rows){<br> //根据数组来添加列表<br> var FileList = $("idFileList"), oFragment = document.createDocumentFragment();<br> //用文档碎片保存列表<br> Each(rows, function(cells){<br> var row = document.createElement("tr");<br> Each(cells, function(o){<br> var cell = document.createElement("td");<br> if(typeof o == "string"){ cell.innerHTML = o; }else{ cell.appendChild(o); }<br> row.appendChild(cell);<br> });<br> oFragment.appendChild(row);<br> })<br> //ie的table不支持innerHTML所以这样清空table<br> while(FileList.hasChildNodes()){ FileList.removeChild(FileList.firstChild); }<br> FileList.appendChild(oFragment);<br> }</p> <p>$("idLimit").innerHTML = fu.Limit;</p> <p>$("idExt").innerHTML = fu.ExtIn.join(",");</p> <p>$("idBtndel").onclick = function(){ fu.Clear(); }</p> <p>//在后台通过window.parent来访问主页面的函数<br> //function Finish(msg){ alert(msg); location.href = location.href; }<br> <SCRIPT><br> [/code]</p> <p>struts配置文件(用的插件自动生成json):<br> [code="java"]<br> <action name="*File" method="{1}" class="com.web.action.UploadFile"><br> <result type="json" name="success"><br> <param name="includeProperties">msg</param><br> <param name="excludeNullProperties">true</param><br> </result><br> <result type="json" name="error"><br> <param name="includeProperties">msg</param><br> <param name="excludeNullProperties">true</param><br> </result><br> </action><br> [/code]</p> <p>后台UploadFile.java的代码<br> [code="java"]<br> package com.web.action;</p> <p>import java.io.BufferedInputStream;<br> import java.io.BufferedOutputStream;<br> import java.io.File;<br> import java.io.FileInputStream;<br> import java.io.FileOutputStream;<br> import java.io.IOException;<br> import java.util.Map;</p> <p>import javax.servlet.http.HttpServletRequest;</p> <p>import org.apache.struts2.ServletActionContext;</p> <p>import com.common.pojo.AppUser;<br> import com.opensymphony.xwork2.ActionContext;</p> <p>/**</p> <ul> <li><p>负责文件的上传<br> */<br> public class UploadFile {<br> private File[] files;<br> private String[] filesFileName;<br> private Map<String, String> filesPath;// 文件名及其上传后路径<br> private String msg;</p> <p>public String upload() {<br> /*<br> * Map session=ActionContext.getContext().getSession(); AppUser<br> * user=(AppUser) session.get("user"); if(user==null){ return "error"; }<br> */<br> msg="上传成功!";<br> for (int i = 0; i < files.length; i++) {<br> fileCopy(files[i], filesFileName[i]);<br> }<br> return "success";<br> }</p> <p>private void fileCopy(File file, String fileName) {<br> BufferedInputStream bis = null;<br> BufferedOutputStream bos = null;<br> String realPath = ServletActionContext.<br> getServletContext().getRealPath("/upload/" + fileName);<br> System.out.println(realPath);<br> try {<br> bis = new BufferedInputStream(new FileInputStream(file));<br> bos = new BufferedOutputStream(new FileOutputStream(new File(<br> realPath)));<br> byte[] buffer = new byte[1024 * 8];<br> int i = -1;<br> while ((i = bis.read(buffer)) != -1) {<br> bos.write(buffer,0,i);<br> }<br> bos.flush();<br> } catch (Exception e) {<br> System.out.println("上传异常!");<br> msg="上传出错!";<br> e.printStackTrace();<br> }finally{<br> try {<br> if (bis != null)<br> bis.close();<br> if (bos != null)<br> bos.close();<br> } catch (IOException e1) {<br> System.out.println("上传结束异常!");<br> e1.printStackTrace();<br> }<br> }<br> }</p> <p>public File[] getFiles() {<br> return files;<br> }</p> <p>public void setFiles(File[] files) {<br> this.files = files;<br> }</p> <p>public String[] getFilesFileName() {<br> return filesFileName;<br> }</p> <p>public void setFilesFileName(String[] filesFileName) {<br> this.filesFileName = filesFileName;<br> }</p> <p>public String getMsg() {<br> return msg;<br> }</p> <p>public void setMsg(String msg) {<br> this.msg = msg;<br> }<br> }<br> [/code]</p></li> </ul>