上传图像后,使用Jsp页面未重定向到ajax调用成功
我创建了一个简单的图像上传系统的帮助下,堆栈流感谢与jsp与ajax。 图像上传成功。但上传图像后,我希望获得ajax成功函数中写入的警告消息,但它不起作用。上传图像后,我只获得[{“成功”:“成功”}] 到目前为止我所尝试的,我附在下面。 index.jsp上传图像后,使用Jsp页面未重定向到ajax调用成功,ajax,jsp,Ajax,Jsp,我创建了一个简单的图像上传系统的帮助下,堆栈流感谢与jsp与ajax。 图像上传成功。但上传图像后,我希望获得ajax成功函数中写入的警告消息,但它不起作用。上传图像后,我只获得[{“成功”:“成功”}] 到目前为止我所尝试的,我附在下面。 index.jsp <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
</head>
<body>
<form id="upload-form" class="upload-box" action="upl.jsp" method="post" enctype="multipart/form-data">
<input type="file" id="file" name="file" />
<input type="submit" id="upload-button" value="upload" />
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Dropify/0.2.2/js/dropify.min.js"></script>
<script>
$(function() {
$('#upload-form').ajaxForm({
success: function(msg) {
alert("File has been uploaded successfully");
},
error: function(msg) {
$("#upload-error").text("Couldn't upload file");
}
});
});
</script>
</body>
</html>
<%@page import="org.json.simple.JSONObject"%>
<%@page import="org.json.simple.JSONArray"%>
<%@ page import="java.io.*,java.sql.*,java.util.zip.*" %>
<%
JSONArray list = new JSONArray();
JSONObject obj = new JSONObject();
String saveFile="";
String contentType = request.getContentType();
if((contentType != null)&&(contentType.indexOf("multipart/form-data") >= 0)){
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while(totalBytesRead < formDataLength){
byteRead = in.read(dataBytes, totalBytesRead,formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
File ff = new File("F:/123/"+saveFile);
FileOutputStream fileOut = new FileOutputStream(ff);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
obj.put("success", "success");
list.add(obj);
out.println(list.toJSONString());
out.flush();
%>
<%
Connection connection = null;
PreparedStatement psmnt = null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection("jdbc:mysql://localhost/ems","root","");
psmnt = connection.prepareStatement("insert into employee(photo) values(?)");
psmnt.setString(1, saveFile);
int s = psmnt.executeUpdate();
}
catch(Exception e){
e.printStackTrace();
}
}
%>
$(函数(){
$(“#上传表单”).ajaxForm({
成功:功能(msg){
警报(“文件已成功上载”);
},
错误:函数(msg){
$(“#上载错误”).text(“无法上载文件”);
}
});
});
upl.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
</head>
<body>
<form id="upload-form" class="upload-box" action="upl.jsp" method="post" enctype="multipart/form-data">
<input type="file" id="file" name="file" />
<input type="submit" id="upload-button" value="upload" />
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Dropify/0.2.2/js/dropify.min.js"></script>
<script>
$(function() {
$('#upload-form').ajaxForm({
success: function(msg) {
alert("File has been uploaded successfully");
},
error: function(msg) {
$("#upload-error").text("Couldn't upload file");
}
});
});
</script>
</body>
</html>
<%@page import="org.json.simple.JSONObject"%>
<%@page import="org.json.simple.JSONArray"%>
<%@ page import="java.io.*,java.sql.*,java.util.zip.*" %>
<%
JSONArray list = new JSONArray();
JSONObject obj = new JSONObject();
String saveFile="";
String contentType = request.getContentType();
if((contentType != null)&&(contentType.indexOf("multipart/form-data") >= 0)){
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while(totalBytesRead < formDataLength){
byteRead = in.read(dataBytes, totalBytesRead,formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
File ff = new File("F:/123/"+saveFile);
FileOutputStream fileOut = new FileOutputStream(ff);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
obj.put("success", "success");
list.add(obj);
out.println(list.toJSONString());
out.flush();
%>
<%
Connection connection = null;
PreparedStatement psmnt = null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection("jdbc:mysql://localhost/ems","root","");
psmnt = connection.prepareStatement("insert into employee(photo) values(?)");
psmnt.setString(1, saveFile);
int s = psmnt.executeUpdate();
}
catch(Exception e){
e.printStackTrace();
}
}
%>
= 0)){
DataInputStream in=新的DataInputStream(request.getInputStream());
int formDataLength=request.getContentLength();
字节数据字节[]=新字节[formDataLength];
int byteRead=0;
int totalBytesRead=0;
而(totalBytesRead
我们必须在upl.jsp页面内做什么才能重定向到ajax成功功能。该警报是否显示?未显示请帮助我。检查您的浏览器
网络选项卡->XHR
查看请求是否成功,它应该显示200
状态未显示任何内容空白显示给我们。该警报是否显示是否显示?未显示请帮助我。检查您的浏览器网络选项卡->XHR
查看请求是否成功。它应显示200
状态未显示任何内容空白显示给我们。