Home > Web Front-end > H5 Tutorial > Introduction to the use of HTML5 FileSystem API_html5 tutorial skills

Introduction to the use of HTML5 FileSystem API_html5 tutorial skills

WBOY
Release: 2016-05-16 15:49:41
Original
1320 people have browsed it

One of the strengths of HTML5 is that it allows web programs to apply for some temporary or permanent space (Quota) where data storage and even file operations can be performed.

FileSystem provides operations such as creating, moving, and deleting folders and files, which greatly facilitates local processing of data. Moreover, all data is in a sandbox (sandboxed), and different web programs cannot access each other. This This ensures data integrity and security.

In the CatWrite project, this feature of HTML5 is used to store data, which is very convenient. However, currently only the Chrome browser supports the FileSystem API better, so it can only run in the Chrome browser.

When completing this function, I consulted a lot of information. Some of the information was a year ago. However, with the changes in browser versions, some codes have become outdated. I will summarize and organize them one by one here. Only the APIs used in the project are listed here, which is a summary of the completed functions.

Apply for space
In order to store data, you must apply to the browser. If it is permanent storage, the user will be asked, and execution will continue only after consent.

First you must declare the desired permissions.

Copy code
The code is as follows:

window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem; //File system request identification
window.resolveLocalFileSystemURL = window.resolveLocalFileSystemURL || window.webkitResolveLocalFileSystemURL; //Obtain the read permission of the file based on the URL

After obtaining the system permission You can apply for space from the browser

Copy the code
The code is as follows:

window. requestFileSystem(window.PERSISTENT, //persistent(permanent) or temporary(temporary)
1024*1024, //1M
onInitFs, //callback function after success
errorHandler); //after error Callback function

Callback function

Copy code
The code is as follows:

function onInitFs(fs){
fs.root.getDirectory('catwrite_documents', {create: true}, function(dirEntry) {
console.log('You have just created the ' dirEntry. name ' directory.');
}, errorHandler);
}
//Error callback
function errorHandler(err){
var msg = 'An error occured: ';
switch (err.code) {
case FileError.NOT_FOUND_ERR:
msg = 'File or directory not found';
break;
case FileError.NOT_READABLE_ERR:
msg = 'File or directory not readable';
break;
case FileError.PATH_EXISTS_ERR:
msg = 'File or directory already exists';
break;
case FileError.TYPE_MISMATCH_ERR:
msg = ' Invalid filetype';
break;
default:
msg = 'Unknown Error';
break;
};
console.log(msg err);
}

If you succeed, you will regret calling the OnInitFs callback function, and use the getDirectory method to create a folder, which will be discussed below.

But there is a problem. If you do this, it will be applied every time the page is loaded. This is definitely not what we want. What we want is to be able to read the data when there is data.

Determine whether space has been applied for
So we need to read the browser's data to see if it has been stored. This uses another API:

Copy the code
The code is as follows:

void queryUsageAndQuota(
in DOMString url,
in EntryCallback successCallback,
in optional ErrorCallback errorCallback
);

This API can query the space status of the current web, if successful The successCallback callback function will be called and the used space and total space will be passed into the method as parameters. If it fails, errorCallback is called.

Copy code
The code is as follows:

window.webkitStorageInfo.queryUsageAndQuota(webkitStorageInfo.PERSISTENT,
function(used, remaining){
if(remaining == ""){
console.log("No space applied. ");
}else{
console.log("used space" used);
console.log("all space" remaining);
}
},
errorHandler);


We can judge whether there is application space by judging the remaining parameter. If there is no application, return to the previous step to apply for space. If there is already space, you need to obtain the space and the file so that you can operate the data.

Get file entry
FileSystem uses a special file system and sandbox mode. Files in the sandbox cannot be accessed on a computer or other web. You can only use the corresponding format. access.

Type in your browser:

?filesystem:http://catcoder.com/persistent/
 

In this way, you can access the permanent data of the catcoder.com website on this machine. Replace persistent with temporary to read the temporary space.

Then we can get the entry of the file through the URL and the corresponding API (Lets you look up the entry for a file or directory with a local URL).

Copy code
The code is as follows:

void resolveLocalFileSystemURL(
in DOMString url ,
in EntryCallback successCallback,
in optional ErrorCallback errorCallback
);

You can read the data stored on the machine below

Copy code
The code is as follows:

var url = "filesystem:http://" window. location.host "/persistent/catwrite_documents/";
window.resolveLocalFileSystemURL(url,function(fileEntry){
console.log(fileEntry);
var dirReader = fileEntry.createReader();
var readEntries = function(){
dirReader.readEntries(function(results){
if(!results.length){
create_file_title("Default file", "");
console.log ("No file!");
}else{
console.log("Read results.length "files");
for(var i = 0; i < results. length; i ){
console.log(results[i].name);
getFileContentByName(fileEntry, results[i].name);
}
}
},errorHandler) ;
};
readEntries();
},errorHandler);
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template