ajax方式提交带文件上传的表单
<wbr><wbr><wbr>一般的表单都是通过ajax方式提交,所以碰到带文件上传的表单就比较麻烦。基本原理就是在页面增加一个隐藏iframe,然后通过ajax提交除文件之外的表单数据,在表单数据提交成功之后的回调函数中,通过form单独提交文件,而这个提交文件的form的target就指向前述隐藏的iframe。</wbr></wbr></wbr>
html 代码
<html>
<wbr><wbr><wbr><br>
<body><wbr><wbr><wbr><br><wbr><wbr><br>
<form action="upload.jsp" id="form1" name="form1"encType="multipart/form-data"<wbr> method="post"<span style="color:#2F3699">target="hidden_frame"</span>><wbr><wbr><wbr><br><wbr><wbr><wbr> <inputtype="file" id="file" name="file"style="width:450"><wbr><wbr><wbr><br><wbr><wbr><wbr> <INPUTtype="submit" value="上传文件"><spanid="msg"></span><wbr><wbr><wbr><br><wbr><wbr><wbr><br><wbr><wbr><wbr><br><wbr><wbr><wbr> <fontcolor="red">支持JPG,JPEG,GIF,BMP,SWF,RMVB,RM,AVI文件的上传</font><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><span style="color:#2F3699"><iframe name='hidden_frame' id="hidden_frame"style='display:none'></iframe></span><wbr><wbr><wbr><br>
</form><wbr><wbr><wbr><br><wbr><wbr><br>
</body><wbr><wbr><wbr><br>
</html><wbr><wbr><wbr><br><wbr><wbr><br>
<scripttype="text/javascript"><wbr><wbr><wbr><br><span style="color:#2F3699">functioncallback(msg)</span><wbr><wbr><wbr><br>
{<wbr><wbr><wbr><br><wbr><wbr><wbr>document.getElementByIdx_x_x("file").outerHTML =document.getElementByIdx_x_x("file").outerHTML;<wbr><wbr><wbr><br><wbr><wbr><wbr>document.getElementByIdx_x_x("msg").innerHTML = "<fontcolor=red>"+msg+"</font>";<wbr><wbr><wbr><br>
}<wbr><wbr><wbr><br>
</script><wbr><wbr><br><p>index.html 中主要要做的就是写一个 form和 iframe ,并把 form 的 target 设为 iframe的名字,注意要把 iframe设为不可见,其他的都是正常的文件上传的写法,这样刷新的页面就是这个隐藏的 Iframe ,而在index.html中是不会有页面刷新的,js的callback方法是回调方法。用于清空文件上传框和显示后台信息,注意清空文件上传框的方法,和普通方法有点不一样。</p>
<p><wbr></wbr></p>
<p>--upload.jsp9Dhjsp 代码<br>
<%@ page language="java" contentType="text/html; charset=gb2312"%><wbr><wbr><wbr><br>
<%@ pageimport="com.jspsmart.upload.SmartUpload"%><wbr><wbr><wbr><br><wbr><wbr><br>
<%<wbr><wbr><wbr><br><wbr><wbr><wbr>//新建一个SmartUpload对象<wbr><wbr><wbr><br><wbr><wbr><wbr> SmartUploadsu = newSmartUpload();<wbr><wbr><wbr><br><wbr><wbr><br><wbr><wbr><wbr>//上传初始化<wbr><wbr><wbr><br><wbr><wbr><wbr>su.initialize(pageContext);<wbr><wbr><wbr><br><wbr><wbr><br><wbr><wbr><wbr> //设定上传限制<wbr><wbr><wbr><br><wbr><wbr><wbr>//1.限制每个上传文件的最大长度。<wbr><wbr><wbr><br><wbr><wbr><wbr>su.setMaxFileSize(10000000);<wbr><wbr><wbr><br><wbr><wbr><br><wbr><wbr><wbr>//2.限制总上传数据的长度。<wbr><wbr><wbr><br><wbr><wbr><wbr>su.setTotalMaxFileSize(20000000);<wbr><wbr><wbr><br><wbr><wbr><br><wbr><wbr><wbr>//3.设定允许上传的文件(通过扩展名限制),仅允许doc,txt文件。<wbr><wbr><wbr><br><wbr><wbr><wbr>su.setAllowedFilesList("doc,txt,jpg,rar,mid,waw,mp3,gif");<wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr> boolean sign= true;<wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr>//4.设定禁止上传的文件(通过扩展名限制),禁止上传带有exe,bat,jsp,htm,html扩展名的文件和没有扩展名的文件。<wbr><wbr><wbr><br><wbr><wbr><wbr> try{<wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>su.setDeniedFilesList("exe,bat,jsp,htm,html");<wbr><wbr><wbr><br><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>//上传文件<wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>su.upload();<wbr><wbr><wbr><br><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></p></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>