action 执行servlet怎样用ajax让它保持在当前页面
主要用来往服务器上传文件的
jsp:
<form id="form1" name="form1" method="POST" action="<%=request.getContextPath()%>/FileUploadServlet" enctype="multipart/form-data" class="L5form" target= "_blank ">
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td class="FieldLabel" width="15%" nowrap="nowrap">文件上传</td>
<td class="FieldInput" width="35%" nowrap="nowrap">
<input name="file" type="file" size="20" title="选择要上传的文件" style="width:90%">
</td>
</tr>
</table>
</form>
js:
function save(){
document.form1.submit();
}
servlet:
public class FileUploadServlet extends HttpServlet {
private static final long serialVersionUID = -7744625344830285257L;
private ServletContext sc;
private String savePath;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void init(ServletConfig config) {
// 在web.xml中设置的一个初始化参数
savePath = config.getInitParameter("savePath");
sc = config.getServletContext();
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String Msg="";
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List items = upload.parseRequest(request);
Iterator itr = items.iterator();
while (itr.hasNext()) {
FileItem item = (FileItem) itr.next();
if (item.isFormField()) {
System.out.println("表单参数名:" + item.getFieldName() + ",表单参数值:" + item.getString("UTF-8"));
} else {
if (item.getName() != null && !item.getName().equals("")) {
//System.out.println("上传文件的大小:" + item.getSize());
//System.out.println("上传文件的类型:" + item.getContentType());
// item.getName()返回上传文件在客户端的完整路径名称
// System.out.println("上传文件的名称:" + item.getName());
File tempFile = new File(item.getName());
//上传文件的保存路径
File file = new File(sc.getRealPath("/") + savePath, tempFile.getName());
// System.out.println(sc.getRealPath("/") + savePath);
item.write(file);
// request.setAttribute("Msg", "上传文件成功!");
}else{
// request.setAttribute("Msg", "没有选择上传文件!");
}
}
}
}catch(FileUploadException e){
// e.printStackTrace();
} catch (Exception e) {
// e.printStackTrace();
// request.setAttribute("Msg", "上传文件失败!");
}
// response.sendRedirect("/jsp/cxsdc/jcycommonpipeprovideunit/uploadResult.jsp").forward(request, response);
//request.getRequestDispatcher("/jsp/cxsdc/jcycommonpipeprovideunit/uploadResult.jsp").forward(request, response);
}
}
servlet中的跳转页面走不通,因为jsp中需要异步显示。我用ajax解决不了,主要是文件流传不过去,如下
$.ajax({
type:"post",
cache:false,
timeout:300000,
dataType:"json",
url:"/ftjdc/FileUploadServlet",
success:function (result) {
var json=eval(result);
alert(json.mesg);
window.location.reload();
},
error:function (result) {
alert("失败");
window.location.reload();
}
});
jquer已经应用。
[解决办法]
上传
[解决办法]
用ajaxfileupload.js试试。
[解决办法]
表单以ajax方式提交 ,jquery的form插件
[解决办法]
其实你也可以在上传页面设置一个隐藏的iframe,之后上传的form 设置target为这个iframe的名字,这样页面就不会跳转了 。
[解决办法]