AJAX不会在文件上传后重定向到网页-POST方法
AJAX does not redirect to a webpage after file uploading - POST method
我目前正在尝试开发一个针对Google Chrome(4及更高版本)优化的Java web应用程序。
我希望用户能够选择几个文件,将它们上传到服务器(使用显示在名为uploadForm的网页中的表单),并在上传完成后自动重定向到另一个网页(称为上传)。
因此,我创建了一个JSP文件(uploadForm.JSP),其中定义了一个上传表单。我还使用XMLHttpRequest对象实现了文件上传过程(这是软件规范的一部分。我别无选择)
<body>
<form id="file_form" action="upload" method="post" enctype="multipart/form-data">
<input type="file" name="file_input[]" id="file_select" multiple webkitdirectory=""/>
<button type="submit" id="upload_button" >Upload</button>
</form>
</body>
<script type="text/javascript">
// Get the form
var form = document.getElementById("file_form");
// Get the file selecter
var fileSelect = document.getElementById("file_select");
// Get the button which allows to upload the documents
var uploadButton = document.getElementById("upload_button");
// Set up the request
var xhr = new XMLHttpRequest();
// Create a new FormData object
var formData = new FormData();
form.onsubmit = function(event){
// Prevent the form to be submitted. We want to write our
// own submission protocol
event.preventDefault();
// Update the button status during the uploading
uploadButton.innerHTML = 'Uploading...';
// Get the selected files from the input
var files = fileSelect.files;
// Loop through each of the selected files
for(var i=0;i<files.length;i++){
// The file contained in the file list
var file = files[i];
// Add the file to the request
formData.append('file_input[]',file,file.name);
xhr.open('POST','upload',true);
// Send the data
xhr.send(formData);
}
};
// Set up a handler for when the request finishes
xhr.onload = function(){
if(xhr.status===200){
// File uploaded
uploadButton.innerHTML = 'Uploaded';
}else{
alert('An error occurred.File was not uploaded');
}
};
</script>
上传完成时。用户会自动重定向到另一个名为"上传"的网页(upload.jsp),并由Servlet(UploadServlet.java
upload.jsp:
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
Everything ok
</body>
</html>
UploadServlet.java:
@WebServlet(urlPatterns = {"/upload"})
public class UploadServlet extends HttpServlet {
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("I am in the servlet");
RequestDispatcher dispatcher=getServletContext().getRequestDispatcher("/WEB-INF/upload.jsp");
dispatcher.forward(request,response);
}
所选文件已正确上载到服务器。然而,当上传完成时,用户实际上不会重定向到"上传"网页。他仍然在"uploadForm"网页上,这不是我所期望的。
你能帮我吗?非常感谢您的回答
ajax调用不会(直接)导致重定向。当您恢复200就绪状态更改时,让javascript进行重定向。您可以考虑让servlet返回文件已正确上载的信息,并检查该信息以进行验证,而不是在servlet中设置转发。。。
相关文章:
- AJAX不会在文件上传后重定向到网页-POST方法
- 将网页上的链接重定向到javascript函数
- 用户在文本框中输入一个数字,并根据使用 PHP 输入的数字重定向到另一个网页
- 更改“将图像另存为”的操作以重定向到相应的网页
- 如何有条件地将引用人重定向到不同的网页
- Firefox弹出窗口“;这个网页被重定向到一个新的位置“;在每个requestPromise ajax调用上,有什么方
- GreaseMonkey:立即重定向到特殊网页
- 使用javascript将网页从日志重定向到域
- 访问另一个网站后重定向回自己的网页
- 安卓网页视图位置重定向
- Visual Studio 13,将HTML5网页重定向到外部网站
- 是可以用户刷新网页并同时重定向到另一个URL
- 如何在会话过期时从 ajax 响应自动重定向网页
- 提交表单后,网页重定向到 servlet URL,但我想重定向到另一个页面
- 从一个网页重定向到另外两个网页
- 每周五在特定时间重定向网页
- 如何在单击输入时重定向到其他网页
- 如何用“重定向”来重定向网页;http错误404”;自动切换到默认网页
- 自动重定向网页从一个javascript在web服务器
- 重定向网页上的关键事件