在這裏我有demo.jsp頁面,我上傳的圖片爲此我寫了jpg,png和gif的javascript驗證碼,但是如果圖像類型不是這樣,我們不能寫入每個類型的條件,所以有任何替代方法來做到這一點?如何驗證只接受(任何)圖像類型文件的表單?
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ImageResizing</title>
<script>
function vlidate() {
var fileName = document.getElementById("Imgfile").value;
var file_extension = fileName.split('.').pop();
var newWidth = document.getElementById("newWidth").value;
var newHeight = document.getElementById("newHeight").value;
//alert(fileName);
//alert(file_extension);
if (file_extension !== "jpg" || file_extension !== "png" || file_extension !== "gif") {
alert("Choose Valid image");
}
else if ((file_extension == "jpg" || file_extension == "png" || file_extension == "gif") && (newWidth.length != "" && newHeight.length != "")) {
//alert("Go ahead");
document.getElementById("uploadImgForm").submit();
}
}
</script>
</head>
<body bgcolor="#ABD3EB">
<%
out.println("<form method='post' action='FileUploadServlet' enctype='multipart/form-data' id='uploadImgForm' name='uploadImgForm'>");
out.println("<h2>Image Resizing</h2>");
out.println("<table>");
out.println("<tr><td><input type='file' id='Imgfile' name='Imgfile'></td></tr>");
out.println("<tr><td></td></tr>");
out.println("<tr><td>New Width:</td><td><input type='text' id='newWidth' name='newWidth'</td></tr>");
out.println("<tr><td>New Height:</td><td><input type='text' id='newHeight' name='newHeight'</td></tr>");
out.println("<tr><td></td></tr>");
out.println("<tr><td><input type='button' value='Upload Image' onclick='vlidate()' ></td></tr>");
//out.println("<tr><td><input type='submit' value='Upload Image' ></td></tr>");
out.println("</table>");
out.println("</form>");
%>
</body>
@pradnyapalan,不要通過編輯回答問題。 – indivisible
圖像文件的驗證遠遠超出驗證擴展。 –