Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a local file exists (HTML5 FS API)

I am developing a Chrome extension and I want to check if a file exists. I don't want to do anything with the file: I only want to check its existence.

If I use a XMLHttpRequest it doesn't work, because it is not allowed for security reasons. So I have to use the HTML5 FS API. The problem is that this API has no method to check if a file exists.

I have a variable called "fileExists", and I would want to know how to change its value from false to true or from true to false depending on the existence of a file (determined by a URL).

Thanks.

like image 483
user2047330 Avatar asked Feb 09 '13 02:02

user2047330


People also ask

What is API file system?

A file system API is an application programming interface through which a utility or user program requests services of a file system. An operating system may provide abstractions for accessing different file systems transparently.

Can JavaScript access local files?

Web browsers (and JavaScript) can only access local files with user permission. To standardize file access from the browser, the W3C published the HTML5 File API in 2014. It defines how to access and upload local files with file objects in web applications.

How check file exist or not in jQuery?

Approach 1: Use ajax() method of jQuery to check if a file exists on a given URL or not. The ajax() method is used to trigger the asynchronous HTTP request. If the file exists, ajax() method will in turn call ajaxSuccess() method else it will call Error function.

How do I check if a file exists in HTML?

The exists() method of the File object returns a boolean value based on the existence of the file in which it was invoked. If the file exists, the method returns true. It returns false if the file does not exist.


1 Answers

use something like:

   function exists(fileName, callback) {
        storageRootEntry.getFile(fileName, {create : false}, function() {
            callback(true);
        }, function() {
            callback(false);
        });
    }

where storageRootEntry is correctly initialized root directory. The function will returns "true" if file exists and "false" otherwise. The key point here is second parameter {create : false}

like image 115
Vasyl Avatar answered Sep 28 '22 09:09

Vasyl