Check if a file exists locally using JavaScript only

后端 未结 10 1639
盖世英雄少女心
盖世英雄少女心 2020-12-03 14:01

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.

相关标签:
10条回答
  • 2020-12-03 14:20

    Since 'Kranu' helpfully advises 'The only interaction with the filesystem is with loading js files . . .', that suggests doing so with error checking would at least tell you if the file does not exist - which may be sufficient for your purposes?

    From a local machine, you can check whether a file does not exist by attempting to load it as an external script then checking for an error. For example:

    <span>File exists? </span>
    <SCRIPT>
        function get_error(x){
            document.getElementsByTagName('span')[0].innerHTML+=x+" does not exist.";
        }
        url="   (put your path/file name in here)    ";
        url+="?"+new Date().getTime()+Math.floor(Math.random()*1000000);
        var el=document.createElement('script');
        el.id="123";
        el.onerror=function(){if(el.onerror)get_error(this.id)}
        el.src=url;
        document.body.appendChild(el);
    </SCRIPT>
    

    Some notes...

    • append some random data to the file name (url+="?"+new Date etc) so that the browser cache doesn't serve an old result.
    • set a unique element id (el.id=) if you're using this in a loop, so that the get_error function can reference the correct item.
    • setting the onerror (el.onerror=function) line is a tad complex because one needs it to call the get_error function AND pass el.id - if just a normal reference to the function (eg: el.onerror=get_error) were set, then no el.id parameter could be passed.
    • remember that this only checks if a file does not exist.
    0 讨论(0)
  • 2020-12-03 14:24

    Javascript cannot access the filesystem and check for existence. The only interaction with the filesystem is with loading js files and images (png/gif/etc).

    Javascript is not the task for this

    0 讨论(0)
  • 2020-12-03 14:24

    An alternative: you can use a "hidden" applet element which implements this exist-check using a privileged action object and override your run method by:

    File file = new File(yourPath);
    return file.exists();
    
    0 讨论(0)
  • 2020-12-03 14:29

    You can use this

    function LinkCheck(url)
    {
        var http = new XMLHttpRequest();
        http.open('HEAD', url, false);
        http.send();
        return http.status!=404;
    }
    
    0 讨论(0)
  • 2020-12-03 14:30

    If you want to check if file exists using javascript then no, as far as I know, javascript has no access to file system due to security reasons.. But as for me it is not clear enough what are you trying to do..

    0 讨论(0)
  • 2020-12-03 14:34

    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.

    0 讨论(0)
提交回复
热议问题