<div class="codetitle"> <span><a style="CURSOR: pointer" data="90470" class="copybut" id="copybut90470" onclick="doCopy('code90470')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code90470"> <br><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <br><html> <br><head> <br><style><!-- <BR>.errorInfo{ <BR>color:red; <BR>padding-left:5px; <BR>} <BR>--></style><style bogus="1"> .errorInfo{ <br>color:red; <br>padding-left:5px; <br>}</style> <br><script type="text/javascript"><!-- <BR>$(function(){ <BR>$("#DataReport").click(function(){ <BR>//上传文件不能为空,格式必须为xsl和xlsx <BR>var flag=true; <BR>var fileObject=$("input[name='file']"); <BR>var filepath=$("input[name='file']").val(); <BR>if(filepath==undefined||$.trim(filepath)==""){ <BR>fileObject.focus().next().text("请选择上传文件!"); <BR>flag=false; <BR>}else{ <BR>var fileArr=filepath.split("\"); <BR>var fileTArr=fileArr[fileArr.length-1].toLowerCase().split("."); <BR>var filetype=fileTArr[fileTArr.length-1]; <BR>if(filetype!="xls"&&filetype!="xlsx"){ <BR>fileObject.focus().next().text("上传文件必须为Excel文件!"); <BR>flag=false; <BR>}else{ <BR>fileObject.next().text(""); <BR>} <BR>} <br><br>//进行其他相关校验 <br><br>if(flag){ <BR>alert("文件上传成功"); <BR>} <BR>}); <BR>}); <br><br>// --></script> <br><br></head> <br><body> <br><form action="#" method="POST" enctype="multipart/form-data"> <br><div><input type="file" name="file"><span class='errorInfo'></span></div> <br><div><input type="button" value="上传" id="DataReport"></div> <br></form> <br></body> <br></html> <br> </div> <br><strong>注:在head中加入jquery的引入语句,博客还是会过滤掉引入的js文件。</strong> <br>javascript中对上传文件格式过滤的方法: <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="82184" class="copybut" id="copybut82184" onclick="doCopy('code82184')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code82184"> <br><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" <BR>"http://www.w3.org/TR/html4/loose.dtd"> <br><html> <br><head> <br><style><!-- <BR>.errorInfo{ <BR>color:red; <BR>padding-left:5px; <BR>} <BR>--></style><style bogus="1"> .errorInfo{ <br>color:red; <br>padding-left:5px; <br>}</style> <br><script type="text/javascript"><!-- <BR>function validateFile(){ <BR>var flag=true; <BR>var fileObject=document.getElementById("file"); <BR>var errorObject=document.getElementById("error"); <BR>var filepath=fileObject.value; <BR>if(filepath==undefined||filepath==""){ <BR>fileObject.focus(); <BR>errorObject.innerText="请选择上传文件!"; <BR>flag=false; <BR>}else{ <BR>var fileArr=filepath.split("\"); <BR>var fileTArr=fileArr[fileArr.length-1].toLowerCase().split("."); <BR>var filetype=fileTArr[fileTArr.length-1]; <BR>if(filetype!="xls"&&filetype!="xlsx"){ <BR>fileObject.focus(); <BR>errorObject.innerText="上传文件必须为Excel文件!"; <BR>flag=false; <BR>}else{ <BR>errorObject.innerText=""; <BR>} <BR>} <BR>//进行其他相关校验 <BR>if(flag){ <BR>alert("文件上传成功"); <BR>} <BR>} <BR>// --></script> <br></head> <br><body> <br><form action="#" method="POST" enctype="multipart/form-data"> <br><div><input type="file" id="file"><span id="error" class='errorInfo'></span></div> <br><div><input type="button" value="上传" id="DataReport" onclick="validateFile()"></div> <br></form> <br></body> <br></html><br> </div>