Using Javascript/jquery to filter the uploaded file format _jquery

Source: Internet
Author: User
Tags file upload
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>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.