|
当使用org.apache.commons.fileupload组件时,你是否遇到过,无论你怎么弄,后台的FileItem 总是为空? 即使你设置了method 为POST, 并且
enctype="multipart/form-data"
提交方式是这样的:
document.frmUpload.action = imgUrl + params;document.frmUpload.submit();页面代码也仔细检查过了
<form name="frmUpload" method="POST" enctype="multipart/form-data">识别范例影像:<input type="file" id="fileImgSample" name="fileImgSample" ><br></form>
后台也收到响应了, ServletFileUpload.isMultipartContent(req);
也返回true了,即是MultipartContent, 为什么FileItem还是为空呢?
这里没有用struts,也没有使用filter, 到底是什么原因呢.
通过一个简单的页面测试可行,终于发现了问题的原因所在.
<form method="POST" enctype="multipart/form-data" action="http://localhost:9080/MyWeb/tempMgrServlet?action=uploadImgSample"><p><input type="file" name="F1" size="20"><input type="submit" value="提交" name="B1"><input type="reset" value="重置" name="B2"></p></form>
一般情况下form submit()后,页面会跳转到指定的页面,但这里不知是什么原因,页面始终没有跳转,但后台却接收到了响应,在如今AJAX 横行的时代, 确实很容易忽略这个问题.
解决方法:
加入iframe
<iframe name="ifrmSubmit" id="ifrmSubmit" width="0" height="0"></iframe>//上传返回页面加载完成事件function onUploadReturn(){var ifrmSubmit = document.getElementById('ifrmSubmit');if("complete" == ifrmSubmit.readyState){var msg = ifrmSubmit.contentWindow.document.body.innerHTML;if(msg != ""){alert('返回消息:' + msg);//$('#w2').window('close'); } }}
将form的 target 指向iframe, 即:
<form name="frmUpload" method="POST" enctype="multipart/form-data" target="ifrmSubmit">识别范例影像:<input type="file" id="fileImgSample" name="fileImgSample" ><br></form>
则该问题得到解决.
|
|
|