Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<style><!--
. errorinfo{
color:red;
padding-left:5px;
}
--></style><style bogus= "1" > errorinfo{
color:red;
padding-left:5px;
}</style>
<script type= "Text/javascript" ><!--
$ (function () {
$ ("#DataReport"). Click (function () {
Upload file cannot be empty, format must be XSL and xlsx
var flag=true;
var fileobject=$ ("input[name= ' file ')");
var filepath=$ ("input[name= ' file ')"). Val ();
if (filepath==undefined| | $.trim (filepath) = = "") {
Fileobject.focus (). Next (). Text (Please select upload file!) ");
Flag=false;
}else{
var filearr=filepath.split ("\ \");
var filetarr=filearr[filearr.length-1].tolowercase (). Split (".");
var filetype=filetarr[filetarr.length-1];
if (filetype!= "xls" &&filetype!= "xlsx") {
Fileobject.focus (). Next (). Text ("The upload file must be an Excel file!"). ");
Flag=false;
}else{
Fileobject.next (). Text ("");
}
}
Perform other related checksums
if (flag) {
Alert ("File upload succeeded");
}
});
});
--></script>
<body>
<form action= "#" method= "POST" enctype= "Multipart/form-data" >
<div><input type= "File" name= "file" ><span class= ' errorinfo ' ></span></div>
<div><input type= "button" value= "Upload" id= "DataReport" ></div>
</form>
</body>
Note: The introduction of jquery into the head, the blog will still filter out the introduction of JS files.
How to filter the uploaded file format in javascript:
Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"
"Http://www.w3.org/TR/html4/loose.dtd" >
<style><!--
. errorinfo{
color:red;
padding-left:5px;
}
--></style><style bogus= "1" > errorinfo{
color:red;
padding-left:5px;
}</style>
<script type= "Text/javascript" ><!--
function Validatefile () {
var flag=true;
var Fileobject=document.getelementbyid ("file");
var Errorobject=document.getelementbyid ("error");
var Filepath=fileobject.value;
if (filepath==undefined| | filepath== "") {
Fileobject.focus ();
errorobject.innertext= "Please select Upload file!" ";
Flag=false;
}else{
var filearr=filepath.split ("\ \");
var filetarr=filearr[filearr.length-1].tolowercase (). Split (".");
var filetype=filetarr[filetarr.length-1];
if (filetype!= "xls" &&filetype!= "xlsx") {
Fileobject.focus ();
errorobject.innertext= "Upload file must be an Excel file!" ";
Flag=false;
}else{
Errorobject.innertext= "";
}
}
Perform other related checksums
if (flag) {
Alert ("File upload succeeded");
}
}
--></script>
<body>
<form action= "#" method= "POST" enctype= "Multipart/form-data" >
<div><input type= "File" id= "file" ><span id= "error" class= ' ErrorInfo ' ></span></div>
<div><input type= "button" value= "Upload" id= "DataReport" onclick= "Validatefile" () ></div>
</form>
</body>