js检验上传文件是否存在

JavaScript09

js检验上传文件是否存在,第1张

判断客户端文件时,可以用 var fso,s=filespec// filespec="C:/path/myfile.txt" fso=new ActiveXObject("Scripting.FileSystemObject")if(fso.FileExists(filespec)) s+=" 文件存在."else s+=" 文件不存在."alert(s)判断服务器端(网...

下面是我用jquery写的选中后立马判断选中的文件的类型,不是的话提示,然后清除,对应的可以自己改成纯js形式:

//文件上传文件选择后事件

$(document).ready(function() {

$("input[id^='fileToUpload']").each(

//这里是用了each因为是多文件上传,input的id都是fileToUpload 开头

function() {

$("#" + $(this).attr("id") + "").live('change',function() {

var fileName = $(this).val()

if (fileName != null&&fileName != "") {

//lastIndexOf如果没有搜索到则返回为-1

if (fileName.lastIndexOf(".") != -1) {

var fileType = (fileName.substring(fileName.lastIndexOf(".") + 1,

fileName.length)).toLowerCase()

var suppotFile = new Array()

suppotFile[0] = "jpg"

suppotFile[1] = "gif"

suppotFile[2] = "bmp"

suppotFile[3] = "png"

suppotFile[4] = "jpeg"

for ( var i = 0i <suppotFile.lengthi++) {

if (suppotFile[i] == fileType) {

if (fileName.length >100) {

alert("文件名长度不能超过100字符")

if (!window.addEventListener) {

document.getElementById(fileName[j]).outerHTML+='' //IE清除inputfile

}else {

document.getElementById(fileName[j]).value = "" //FF清除inputfile

}

return false

}

return true

} else {

continue

}

}

alert("文件类型不合法,只支持 jpg、gif、png、jpeg类型!")

if (!window.addEventListener) {

document.getElementById(fileName[j]).outerHTML+='' //IE

}else {

document.getElementById(fileName[j]).value = "" //FF

}

return false

} else {

alert("文件类型不合法,只支持 jpg、gif、png、jpeg类型!")

if (!window.addEventListener) {

document.getElementById(fileName[j]).outerHTML+='' //IE

}else {

document.getElementById(fileName[j]).value = "" //FF

}

return false

}

}

})

})

})

您好!很高兴为您答疑。

因为js安全机制的问题,您的需求在火狐浏览器下是无法通过纯js实现的,一般会采用flash中间件来变相实现该需求,请知晓。

如果对我们的回答存在任何疑问,欢迎继续问询。