js校验上传文件的格式

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="j" uri="http://java.sun.com/jsp/jstl/core"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <script type="text/javascript"> function validateFileType(){ var obj = document.getElementById("files"); if(obj.value == "" || obj.value == null){ alert("请选择上传文件!"); return ; } //获取文件的后缀名的位置 var index = obj.value.lastIndexOf("."); if(index == -1){ alert("上传文件类型不对!"); return; } //截取后缀名 var type = obj.value.substring(index+1); if(type != "jpg" && type != "png" && type != "jpeg"){ alert("请选择图片文件上传!"); return; }else{ alert("您对了!"); } } </script> </head> <body> <h1>校验上传文件类型</h1> <form action="" > <input type="file" name="files" id="files" value="" ></input> <input type="button" onclick="validateFileType()" name="" value="测试"> </form> </body> </html>


                            

你可能感兴趣的:(上传文件)