仅使用JavaScript检查文件是否存在于本地 [英] Check if a file exists locally using JavaScript only

查看:129
本文介绍了仅使用JavaScript检查文件是否存在于本地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查文件是否存在于本地,即HTML文件所在的位置。它必须是JavaScript。 JavaScript永远不会被禁用。顺便说一下,我为Mac制作了一个钛应用程序,所以我正在寻找一种方法来保护我的文件,使其免受点击show package contents。

解决方案

您的问题是不明确的,因此根据您真正尝试的方式有多种可能的答案如果你正在开发我正在猜测使用Titanium的桌面应用程序,那么你可以使用FileSystem模块的getFile来获取文件对象,然后检查如果它存在使用存在的方法。



这是一个从Appcelerator网站采取的例子:

  var homeDir = Titanium.Filesystem.getUserDirectory(); 
var mySampleFile = Titanium.Filesystem.getFile(homeDir,'sample.txt');

if(mySampleFile.exists()){
alert('一个名为sample.txt的文件已经存在于你的主目录中。
...
}

检查 $ b

然后存在方法参考文档



对于那些认为他在问一个通常的Web开发情况的人,那么这是我给出的两个答案:

1)您想检查服务器端文件是否存在。
在这种情况下,您可以使用ajax请求尝试获取文件并对收到的答案作出反应。虽然,请注意,您只能检查Web服务器公开的文件。一个更好的方法是编写一个服务器端脚本(例如php),它会为你做检查,给定一个文件名并通过ajax调用该脚本。另外,请注意,如果您不够小心,可以非常轻松地在您的应用程序/服务器中创建安全漏洞。

2)您想检查客户端边文件存在。
在这种情况下,正如其他人所指出的那样,由于安全原因(尽管IE过去通过ActiveX和Scripting.FileSystemObject类允许这样做)是不允许的,并且这很好(没人希望你能够通过他们的文件),所以忘记这一点。


I want to check if a file exists locally, where the HTML file is located. It has to be JavaScript. JavaScript will never be disabled. jQuery is not good but can do.

By the way, I am making a titanium app for Mac so I am looking for a way of protecting my files from people who click "show package contents".

解决方案

Your question is ambiguous, so there are multiple possible answers depending on what you're really trying to achieve.

If you're developping as I'm guessing a desktop application using Titanium, then you can use the FileSystem module's getFile to get the file object, then check if it exists using the exists method.

Here's an example taken from the Appcelerator website:

var homeDir = Titanium.Filesystem.getUserDirectory();
var mySampleFile = Titanium.Filesystem.getFile(homeDir, 'sample.txt');

if (mySampleFile.exists()) {
    alert('A file called sample.txt already exists in your home directory.');
    ...
}

Check the getFile method reference documentation

And the exists method reference documentation

For those who thought that he was asking about an usual Web development situation, then thse are the two answers I'd have given:

1) you want to check if a server-side file exists. In this case you can use an ajax request try and get the file and react upon the received answer. Although, be aware that you can only check for files that are exposed by your web server. A better approach would be to write a server-side script (e.g., php) that would do the check for you, given a filename and call that script via ajax. Also, be aware that you could very easily create a security hole in your application/server if you're not careful enough.

2) you want to check if a client-side file exists. In this case, as pointed you by others, it is not allowed for security reasons (although IE allowed this in the past via ActiveX and the Scripting.FileSystemObject class) and it's fine like that (nobody wants you to be able to go through their files), so forget about this.

这篇关于仅使用JavaScript检查文件是否存在于本地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆