Is it possible to check if a file exists on disk using Javascript (not running in a browser)?

落花浮王杯 提交于 2020-01-03 17:12:46

问题


I am using an app that uses only JavaScript as its scripting language. I have a .ini file and I need to see if it exists. Is this possible?

Also, if it doesn't exist, how can I halt the execution?


回答1:


"Yes", assuming ActiveX can be used.

See FileSystemObject aka "FSO" (the FileExists method in particular). FSO is part of Windows Scripting.

It is also possible to use the MSXML load method to access a "file://" and catch the appropriate error. (I don't know if a vanilla XmlHttpRequest request of "file://" can be used here, and/or in what contexts... it might differ between the XHR from MSXML and the one baked into IE7/8 as well.)

Happy coding.


If the JavaScript runs from an HTA/HTML Aplication or Windows Sidgebar Gadget, etc, then it's in "Security Level 0" or "Demigod Mode".

On the other hand, an HTA runs as a fully trusted application and therefore has more privileges than a normal HTML file...




回答2:


Ok doing something like this works:

var file = new File(datafile);
var result = file.open ('r');

if result is true, then the file exists, otherwise false means the file does not exist.




回答3:


Try this:

var myfile = new File(myfile_path);

if (!myfile.exists) {
    alert(myfile + " could not be found!");
}


来源:https://stackoverflow.com/questions/7338119/is-it-possible-to-check-if-a-file-exists-on-disk-using-javascript-not-running-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!