最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - HTML5 File API simple check if file exists - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question asked May 6, 2012 at 15:46 TarangTarang 76k39 gold badges219 silver badges279 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论