js 判断文件格式
functionisValidateFile(obj){varextend=obj.value.substring(obj.value.lastIndexOf(".")+...
function isValidateFile(obj){
var extend = obj.value.substring(obj.value.lastIndexOf(".")+1);
if(extend==""){
}else{
if(!(extend=="xls"||extend=="doc")){
alert("请上传后缀名为xls或doc的文件!");
var nf = obj.cloneNode(true);
nf.value='';
obj.parentNode.replaceChild(nf, obj);
return false;
}
}
return true;
}
</script>
<form id="form1" name="form1" enctype="multipart/form-data" method="post" action="">
<label for="filefield"></label>
<input type="file" name="File1" id="File1" />
<input type="button" name="button" id="button" value="检测上传文件类型" onclick="isValidateFile('File1');" />
</form>
求帮忙查看错误 展开
var extend = obj.value.substring(obj.value.lastIndexOf(".")+1);
if(extend==""){
}else{
if(!(extend=="xls"||extend=="doc")){
alert("请上传后缀名为xls或doc的文件!");
var nf = obj.cloneNode(true);
nf.value='';
obj.parentNode.replaceChild(nf, obj);
return false;
}
}
return true;
}
</script>
<form id="form1" name="form1" enctype="multipart/form-data" method="post" action="">
<label for="filefield"></label>
<input type="file" name="File1" id="File1" />
<input type="button" name="button" id="button" value="检测上传文件类型" onclick="isValidateFile('File1');" />
</form>
求帮忙查看错误 展开
推荐于2016-10-23 · 知道合伙人互联网行家
关注
展开全部
实现如下:
用js对上传的文件大小以及格式进行初步的判断,在服务端再进行一次判断
//判断照片大小
function getPhotoSize(obj){
photoExt=obj.value.substr(obj.value.lastIndexOf(".")).toLowerCase();//获得文件后缀名
if(photoExt!='.jpg'){
alert("请上传后缀名为jpg的照片!");
return false;
}
var fileSize = 0;
var isIE = /msie/i.test(navigator.userAgent) && !window.opera;
if (isIE && !obj.files) {
var filePath = obj.value;
var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
var file = fileSystem.GetFile (filePath);
fileSize = file.Size;
}else {
fileSize = obj.files[0].size;
}
fileSize=Math.round(fileSize/1024*100)/100; //单位为KB
if(fileSize>=10){
alert("照片最大尺寸为10KB,请重新上传!");
return false;
}
}
jsp页面:
<input type="file" id="imgFile" name="upload" style="width:150px;" onchange="getPhotoSize(this)"/>
用js对上传的文件大小以及格式进行初步的判断,在服务端再进行一次判断
//判断照片大小
function getPhotoSize(obj){
photoExt=obj.value.substr(obj.value.lastIndexOf(".")).toLowerCase();//获得文件后缀名
if(photoExt!='.jpg'){
alert("请上传后缀名为jpg的照片!");
return false;
}
var fileSize = 0;
var isIE = /msie/i.test(navigator.userAgent) && !window.opera;
if (isIE && !obj.files) {
var filePath = obj.value;
var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
var file = fileSystem.GetFile (filePath);
fileSize = file.Size;
}else {
fileSize = obj.files[0].size;
}
fileSize=Math.round(fileSize/1024*100)/100; //单位为KB
if(fileSize>=10){
alert("照片最大尺寸为10KB,请重新上传!");
return false;
}
}
jsp页面:
<input type="file" id="imgFile" name="upload" style="width:150px;" onchange="getPhotoSize(this)"/>
展开全部
应该是浏览器不兼容,另找了一个,试一下。如下:
<form id="form1" name="form1" enctype="multipart/form-data" method="post" action="">
<label for="filefield"></label>
<input type="file" name="file" id="file" />
<input type="button" name="button" id="button" value="检测上传文件类型" onclick="checkImg();"/>
</form>
<script>
function checkImg(){
var fileWrong = "error" ;
var s =document.forms[0].file.value;
alert(s) ;
var isIE = navigator.userAgent.indexOf("MSIE");
s = s.substr(s.lastIndexOf(".")+1);
s = s.toLowerCase();
if(s!= "jpg" && s!= "png" && s!= "gif" && s!= "bmp"){
alert(fileWrong);
return false;
}
var fileSize = 0;
if (isIE != -1) {
// var filePath = s;
// var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
// var file = fileSystem.GetFile (filePath);
// fileSize = file.Size;
var image = new Image();
image.src = s;
fileSize = image.fileSize;
} else {
fileSize = document.forms[0].pImg.files[0].size;
}
if(fileSize > 500000){
alert(size500);
return false;
}
return true;
}
</script>
<form id="form1" name="form1" enctype="multipart/form-data" method="post" action="">
<label for="filefield"></label>
<input type="file" name="file" id="file" />
<input type="button" name="button" id="button" value="检测上传文件类型" onclick="checkImg();"/>
</form>
<script>
function checkImg(){
var fileWrong = "error" ;
var s =document.forms[0].file.value;
alert(s) ;
var isIE = navigator.userAgent.indexOf("MSIE");
s = s.substr(s.lastIndexOf(".")+1);
s = s.toLowerCase();
if(s!= "jpg" && s!= "png" && s!= "gif" && s!= "bmp"){
alert(fileWrong);
return false;
}
var fileSize = 0;
if (isIE != -1) {
// var filePath = s;
// var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
// var file = fileSystem.GetFile (filePath);
// fileSize = file.Size;
var image = new Image();
image.src = s;
fileSize = image.fileSize;
} else {
fileSize = document.forms[0].pImg.files[0].size;
}
if(fileSize > 500000){
alert(size500);
return false;
}
return true;
}
</script>
更多追问追答
追问
谢谢 我想请问一下常用的浏览器有哪些
追答
IE,腾迅TT,MAXTHON ,FIREFOX, OPERA ,世界之窗,Netscape,ucfly
等等等等。。。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
应该是浏览器不兼容,另找了一个,试一下。如下:
<form id="form1" name="form1" enctype="multipart/form-data" method="post" action="">
<label for="filefield"></label>
<input type="file" name="file" id="file" />
<input type="button" name="button" id="button" value="检测上传文件类型" onclick="checkImg();"/>
</form>
<script>
function checkImg(){
var fileWrong = "error" ;
var s =document.forms[0].file.value;
alert(s) ;
var isIE = navigator.userAgent.indexOf("MSIE");
s = s.substr(s.lastIndexOf(".")+1);
s = s.toLowerCase();
if(s!= "jpg" && s!= "png" && s!= "gif" && s!= "bmp"){
alert(fileWrong);
return false;
}
var fileSize = 0;
if (isIE != -1) {
// var filePath = s;
// var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
// var file = fileSystem.GetFile (filePath);
// fileSize = file.Size;
var image = new Image();
image.src = s;
fileSize = image.fileSize;
} else {
fileSize = document.forms[0].pImg.files[0].size;
}
if(fileSize > 500000){
alert(size500);
return false;
}
return true;
}
</script>
<form id="form1" name="form1" enctype="multipart/form-data" method="post" action="">
<label for="filefield"></label>
<input type="file" name="file" id="file" />
<input type="button" name="button" id="button" value="检测上传文件类型" onclick="checkImg();"/>
</form>
<script>
function checkImg(){
var fileWrong = "error" ;
var s =document.forms[0].file.value;
alert(s) ;
var isIE = navigator.userAgent.indexOf("MSIE");
s = s.substr(s.lastIndexOf(".")+1);
s = s.toLowerCase();
if(s!= "jpg" && s!= "png" && s!= "gif" && s!= "bmp"){
alert(fileWrong);
return false;
}
var fileSize = 0;
if (isIE != -1) {
// var filePath = s;
// var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
// var file = fileSystem.GetFile (filePath);
// fileSize = file.Size;
var image = new Image();
image.src = s;
fileSize = image.fileSize;
} else {
fileSize = document.forms[0].pImg.files[0].size;
}
if(fileSize > 500000){
alert(size500);
return false;
}
return true;
}
</script>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
传的参数不对
function isValidateFile(obj) {
var extend = form1.File1.value.substring(form1.File1.value.lastIndexOf(".") + 1);
alert(extend);
if (extend == "") {
}
else {
if (!(extend == "xls" || extend == "doc")) {
alert("请上传后缀名为xls或doc的文件!");
return false;
}
}
return true;
function isValidateFile(obj) {
var extend = form1.File1.value.substring(form1.File1.value.lastIndexOf(".") + 1);
alert(extend);
if (extend == "") {
}
else {
if (!(extend == "xls" || extend == "doc")) {
alert("请上传后缀名为xls或doc的文件!");
return false;
}
}
return true;
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |