HTML5 File API简单检查文件是否存在 [英] HTML5 File API simple check if file exists

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

问题描述

我有一个临时的文件API存储(HTML5),但我无法检查文件是否存在。有没有简单的方法来检查它?我是否必须实际尝试阅读该文件才能找到?

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

这可能是同步检查吗?

推荐答案

您必须阅读该文件。以下示例基于来自HTML5Rocks的此演示(它捕获所有错误,您可能想要过滤不同的错误类型):

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);

同步方法仅适用于Web Workers,因为它们具有阻塞性质。错误处理是略有不同

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

这篇关于HTML5 File API简单检查文件是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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