简体   繁体   English

使用Cordova文件插件

[英]Using the Cordova file plugin

In my Android Cordova app I want to serve frequently used image files from a local cache in order to save bandwidth and/or when no network is available. 在我的Android Cordova应用程序中,我想从本地缓存中提供常用的图像文件,以节省带宽和/或在没有网络可用时使用。 The Cordova File plugin appears to be well suited to this job. Cordova File插件似乎非常适合此工作。 However, I am having some difficulty establishing just how I go about creating cached image files once they have been acquired from an external source. 但是,一旦从外部来源获取缓存的图像文件,我就很难确定如何创建它们。 The steps as far as I can see are the following 据我所知,步骤如下

  • Acquire the external file 获取外部文件
  • Once it is available use window.requestFileSystem to get access to the sandboxed file system. 一旦可用,请使用window.requestFileSystem来访问沙盒文件系统。 This returns the file system object which has as its filesystem attribute the object `{name:"persistent",rood:DirectoryEntry} 这将返回文件系统对象,该文件系统对象具有对象`{name:“ persistent”,rood:DirectoryEntry}作为其filesystem属性。
  • Pass the directoryentry above along with the acquired image blob to fs.root.getFile('example.png', {create: true, exclusive: true},fileCreated,fileCreationFailed) ... 将上面的目录条目以及获取的图像Blob传递到fs.root.getFile('example.png',{create:true,Exclusive:true},fileCreated,fileCreationFailed)...

It is the last step that is not clear to me. 这是我不清楚的最后一步。 From my reading of the plugin documentation the right location to create the cached image file is cache folder. 根据我对插件文档的阅读,创建缓存图像文件的正确位置是cache文件夹。 However, if I were to issue the above root.getFile I would surely be attempting to create the image file in the root folder - which would probably fail because that location is not read-write. 但是,如果要发布上面的root.getFile我肯定会尝试在根文件夹中创建映像文件-可能会失败,因为该位置不是读写位置。

I am either misunderstanding something here or there is a bit of documentation that is missing. 我在这里误解了某些内容,或者缺少一些文档。 In either case I hope that someone here will be able to put me on the right track. 无论哪种情况,我都希望这里的某人能够使我走上正确的道路。

As outlined in the cordova-plugin-file documentation you should be able to use the window.TEMPORARY argument when calling window.requestFileSystem get to a reference to the application cache folder which you can then use to read/write files: cordova-plugin-file文档中所述,在调用window.requestFileSystem时,您应该能够使用window.TEMPORARY参数,以获取对应用程序缓存文件夹的引用,然后您可以使用该文件夹读取/写入文件:

window.requestFileSystem(window.TEMPORARY, 5 * 1024 * 1024, function (fs) {
    console.log('file system open: ' + fs.name);
    createFile(fs.root, "newTempFile.txt", false);
}, onErrorLoadFs);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM