javascript 是否可以使用Javascript(不在浏览器中运行)检查磁盘上是否存在文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7338119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to check if a file exists on disk using Javascript (not running in a browser)?
提问by Joan Venge
I am using an app that uses onlyJavaScript as its scripting language. I have a .ini
file and I need to see if it exists. Is this possible?
我正在使用一个仅使用JavaScript 作为其脚本语言的应用程序。我有一个.ini
文件,我需要看看它是否存在。这可能吗?
Also, if it doesn't exist, how can I halt the execution?
另外,如果它不存在,我该如何停止执行?
采纳答案by Joan Venge
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.
如果result
是true
,则该文件存在,否则false
表示该文件不存在。
回答by faham
Try this:
试试这个:
var myfile = new File(myfile_path);
if (!myfile.exists) {
alert(myfile + " could not be found!");
}
回答by faham
"Yes", assuming ActiveX can be used.
“是”,假设可以使用 ActiveX。
See FileSystemObjectaka "FSO" (the FileExistsmethod in particular). FSO is part of Windows Scripting.
请参阅FileSystemObject又名“FSO”(特别是FileExists方法)。FSO 是 Windows 脚本的一部分。
It is also possible to use the MSXML load methodto access a "file://" and catch the appropriateerror. (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.)
还可以使用 MSXML 加载方法访问“file://”并捕获相应的错误。(我不知道这里是否可以使用“file://”的普通 XmlHttpRequest 请求,和/或在什么上下文中......它可能在来自 MSXML 的 XHR 和烘焙到 IE7/8 的 XHR 之间有所不同.)
Happy coding.
快乐编码。
If the JavaScript runs from an HTA/HTML Aplicationor Windows Sidgebar Gadget, etc, then it's in "Security Level 0" or "Demigod Mode".
如果 JavaScript 从HTA/HTML 应用程序或 Windows Sidgebar Gadget 等运行,则它处于“安全级别 0”或“半神模式”。
On the other hand, an HTA runs as a fully trusted applicationand therefore has more privileges than a normal HTML file...
另一方面,HTA 作为完全受信任的应用程序运行,因此比普通 HTML 文件具有更多权限......