6

I have a temporary File API store (HTML5) but I can't check whether a file exists or not. Is there a simple way to check it? Do I have to actually try and read the file to find out?

A search around has yielded me nothing concrete

A synchronous check would be nice is this possible?

1 Answer 1

7

You have to read the file. The following example is based on this demo from HTML5Rocks (it catches all errors, you might want to filter the different error types):

    var errorHandler = function() {
        // File is not readable or does not exist!
    };
    fs.root.getFile('log.txt', {}, function(fileEntry) {
        fileEntry.file(function(file) {
            var reader = new FileReader();
            reader.onloadend = function() {
                // The file exists and is readable
            };
            reader.readAsText(file);
        }, errorHandler);
    }, errorHandler);

The synchronous method is only available to Web Workers, due to their blocking nature. The error handling is slightly different.

Sign up to request clarification or add additional context in comments.

1 Comment

on the errorHandler i recive only the error... but i want download the file if doesn't exist

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.