0
以下是在使用Struts框架Ajax請求從JSP文件發送到servlet中
jsp的
<html:form action="/uploadDrawing" method="post" enctype="multipart/form-data">
<input type="file" name="attachfile" class="regi_textbox"/>
<input type="submit" name="button" class="update_but" value="Upload File" />
</html:form>
形式
private FormFile attachfile;
public FormFile getAttachfile() {
return attachfile;
}
public void setAttachfile(FormFile attachfile) {
this.attachfile = attachfile;
}
Action類
FormFile attachfile = uploadDrawingForm.getAttachfile();
這個工程完成對我來說很好,但是在EED做到這一點使用Ajax請求(JSP-servlet的),下面是我的嘗試沒有成功---
jsp的
<script>
function dynamicUpload()
{
alert("function played");
var fd = new FormData($("attachfileform"));
fd.append('file', input.files[0]);
alert(fd);
$.ajax({
url: 'UploadDrawingServlet',
data: fd,
processData: false,
contentType: false,
type: 'POST',
success: function(data){
alert(data);
}
});
}
</script>
<form enctype="multipart/form-data" method="post" action="" id="attachfileform" name="attachfileform" >
<input type="file" name="attachfile" class="regi_textbox"/>
<input type="button" class="update_but" value="Upload File" onclick="dynamicUpload()"/>
</form>
的Servlet
public class UploadDrawingServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String file = request.getParameter("data");
}
}
對於映射在web.xml我已經提供
<servlet>
<servlet-name>UploadDrawingServlet</servlet-name>
<servlet-class>UploadDrawingServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UploadDrawingServlet</servlet-name>
<url-pattern>/UploadDrawingServlet</url-pattern>
</servlet-mapping>
而在servlet類其接收作爲---
public class UploadDrawingServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Here in servlet class");
String file = request.getParameter("data");
}
}
有誰可以告訴我如何在使用ajax請求後進行提示。 或者如果這種類型的請求是不可能的。 謝謝////
這是行不通的。控制檯沒有錯誤//// – NewBee 2013-05-04 10:19:04
什麼特別不起作用? FormData對象不正確,或者ajax post請求沒有發送到服務器,或者您收到不正確的響應? – 2013-05-04 10:29:27
確保$ .ajax中的url正確。我懷疑UploadDrawingServlet是否正確。 – 2013-05-04 10:36:41