分享下javascript判斷文件是否存在的方法。
1,判斷客戶端文件時,可以用
var fso,s=filespec; // filespec="C:/path/myfile.txt"fso=new ActiveXObject("Scripting.FileSystemObject");if(fso.FileExists(filespec))s+=" exists.";else // www.49028c.coms+=" doesn't exist.";alert(s);
2,判斷服務器端(網絡文件)時,可以用
var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");xmlhttp.open("GET",yourFileURL,false);xmlhttp.send();if(xmlhttp.readyState==4){if(xmlhttp.status==200)s+=" exists."; //url存在else if(xmlhttp.status==404)s+=" doesn't exist."; //url不存在else s+="";//其他狀態} // www.yuju100.comalert(s);
可以<input style="width:100%" type="file" name="" id="" contentEditable="false" >把contentEditable設置成false限制用戶只能選擇文件,而不能隨便輸入.
新聞熱點
疑難解答