How to filter uploaded file formats using javascript/jquery_jquery
May 16, 2016 pm 06:49 PM
javascript
jquery
upload files
Copy code The code is as follows:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<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(){
//The uploaded file cannot be empty, and the 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 choose to upload a 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 uploaded file must be Excel File! ");
flag=false;
}else{
fileObject.next().text("");
}
}
//Proceed Other related checks
if(flag){
alert("File uploaded successfully");
}
});
});
/ / --></script>
</head>
<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>
< /html>
Note: If you add the jquery import statement in the head, the blog will still filter out the imported js files.
How to filter uploaded file formats in JavaScript:
Copy code The code is as follows:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<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 a file to upload!";
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="The uploaded file must be an Excel file! ";
flag=false;
}else{
errorObject.innerText="";
}
}
//Perform other related verifications
if(flag) {
alert("File uploaded successfully");
}
}
// --></script>
</head>
<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>
</html>
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot tools Tags

Hot Article
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Detailed explanation of jQuery reference methods: Quick start guide

How to use PUT request method in jQuery?

How to remove the height attribute of an element with jQuery?

jQuery Tips: Quickly modify the text of all a tags on the page

Use jQuery to modify the text content of all a tags

Understand the role and application scenarios of eq in jQuery

How to tell if a jQuery element has a specific attribute?

Summary of commonly used file operation functions in PHP
