表单提交:
1 使用submit提交按钮
<input type="submit" value="submit"\>
2 使用submit函数提交
$("formid").submit();
$("form").submit(function(e){
alert("Submitted");
});
可以定义一个函数,当表单提交时执行的函数,这个只是适用于表单提交
3 ajax方法
$.ajax({
cache: true,
type: "POST",
url:"servlet/imageupload",
async: false,
success: function(data) {
alert($('#uploadform').serialize());
}
});
这样可以实现提交后执行的函数
文件上传:
最简单的方式是使用ajaxfileupload.js插件
1 使用submit提交按钮
<input type="submit" value="submit"\>
2 2 使用submit函数提交
$("formid").submit();
这里submit不能带有参数
3 如果想实现提交后执行函数,比如图片上传后在本地展示
具体说明submit提交后,targent指向iframe,iframe刷新执行相关的函数
function uploadImage()
{
$('#uploadform').submit();
jQuery('#' + "uploadiframe").load(uploadCallback );
}
function uploadCallback()
{
var io = document.getElementById("uploadiframe");
var xml={};
var data;
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;
}
else 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;
}
data = xml.responseText;
data = eval( "(" +data+")" );//eval("data="+data);
$("#simulate_map").attr("src",data["path"]).show();
}
jsp
<form action="servlet/imageupload" method="post" enctype="multipart/form-data" id="uploadform" target="uploadiframe">
<div >
<input type="file" name="file1" οnchange="uploadImage()"/>
<div
style="height: 140px; width: 220px; margin:auto;position:relative;">
<img id="simulate_map" style="width: 220px; height: 140px; " />
</div>
</div>
</form>
<iframe name="uploadiframe" id="uploadiframe" style="display:none">
</iframe>
java
private void upload(HttpServletRequest request, HttpServletResponse response) {
// 用于存放上传文件的目录
String directory = "TEMP";
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(4096);
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("utf-8");
upload.setSizeMax(100 * 1024 * 1024);
try {
List<?> items = upload.parseRequest(request);
Iterator<?> it = items.iterator();
while (it.hasNext()) {
FileItem item = (FileItem) it.next();
if (!item.isFormField()) {
int length = item.getName().replace('\\', '/').split("/").length;
String name = item.getName().replace('\\', '/').split("/")[length - 1];
System.out.println(name);
File dir = new File(request.getSession().getServletContext().getRealPath(directory));
if (!dir.isDirectory() && !dir.mkdirs()) {
return;
}
File putFile = new File(request.getSession().getServletContext().getRealPath(directory)
+ File.separator + name);
// 写文件
item.write(putFile);
// 设置返回响应
response.setContentType("text/html");
response.setStatus(HttpServletResponse.SC_OK);
String basePath = request.getScheme() + "://" + request.getServerName() + ":"
+ request.getServerPort() + request.getContextPath() + "/";
JSONObject jsonObject = new JSONObject();
jsonObject.put("path", basePath + "/" + directory + "/" + name);
PrintWriter out = response.getWriter();
out.write(jsonObject.toString());
out.flush();
out.close();
}
}
} catch (Exception e) {
// TODO: handle exception
}
}