HTML5 File API simple check if file exists

倾然丶 夕夏残阳落幕 提交于 2019-12-19 07:26:13

问题


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:


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.



来源:https://stackoverflow.com/questions/10471866/html5-file-api-simple-check-if-file-exists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!