write a file using FileSystem API

无人久伴 提交于 2019-12-23 12:58:35

问题


I am trying to create a file using the File system API..i googled and i get a code

function onFs(fs) {

  fs.root.getFile('log.txt', {create: true, exclusive: true},
      function(fileEntry) {
           fileEntry.getMetaData(function(md) {
            }, onError);

      },
      onError
  );
}

window.requestFileSystem(TEMPORARY, 1024*1024 /*1MB*/, onFs, onError);

can any one say what is the fs which is passed as function argument..

Please refer me a good example...


回答1:


fs is a javascript object that allows you to make "system-like" level calls to a virtual filesystem.

So for instance you can use the fs object to create/get a reference to a file in the virtual filesystem with fs.root.getFile(...). The third argument (in your case, the following lines of code from your above snippet) in the .getFile(...) method happens to be a callback for successful obtaining of a file reference.

function(fileEntry) {
       fileEntry.getMetaData(function(md) {
        }, onError);
}

That file reference (in your case it is called fileEntry) can have various methods called such as .createWriter(...) for writing to files, .file(...) for reading files and .remove(...) for removing files. Your method calls .getMetaData(...) which contains a file size and modification date.

For more specifics as well as some good examples on the html5 file-system api you may find the following article helpful Exploring the File-System API

The location of the files differs on the browser, operating system and storage type (persistent vs. temporary) but the following link has served to be quiet useful as well Chrome persistent storage locations



来源:https://stackoverflow.com/questions/20877144/write-a-file-using-filesystem-api

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