2 Replies Latest reply on Jan 22, 2017 9:27 AM by SubLock69

    Is there an easier way of accessing files locally


      I am fairly new to PhoneGap, and have read through the documentation time and time again to gain a deeper understanding, but I am still having a hard time understanding how to exactly use the File and FileTransfer APIs. I need to figure out 1: how to make it easier to so I can just write something like `file.get(file_path,error)`,`file.read(type,error)`, and `file.write(text/binary,error)`. `file` is the file object itself for read and write. The `file` used in `file.get` is the object that would return the file object file,  having properties of normal files. And 2: how to pull file data locally from the file system, but from the root of the app, not the system. like local app contents, like how CSS and JS are pulled relative to the HTML file. If anyone can fill me in on these things and whether they can be done easily or not, that would be greatly appreciated!

        • 1. Re: Is there an easier way of accessing files locally
          kerrishotts Adobe Community Professional

          The File API is used for local persistent storage. The File Transfer API is used when you want to transfer local data to a remote server (or vice versa). Note that you can use XHR + the File API to do what the File Transfer API does.


          I'm no fan of the File API itself (not faulting Cordova here -- the spec was a browser standard). There are wrappers that simplify matters (especially if they promisify the API), but I do highly suggest you work with the lower-level interface until you're comfortable with everything going on underneath, because any wrapper will inevitably abstract away errors, and when debugging you'll need to know what's happening at the lower levels.


          Furthermore, you should note that the File API is asynchronous, and there's no way it could be otherwise with Cordova. As such your examples would need to be promises at least "file.get().then...".


          Finally, the File API (and most plugins) allow you to use URI schemes to access your app's sandbox. For example, "cdvfile://localhost/persistent/file.txt". If you're using "file://" schemes (relative OR absolute, explicit or implicit), you shouldn't unless there's no other way -- those are platform dependent and extremely fragile. Remember, when using the File API, there is no "relative" to your HTML file. The URI scheme you use will determine the root of the directory structure you work in and you can work "relative" within that structure.


          You should also pay attention to window.resolveLocalFileSystemURL(), since it does allow you access to locations that don't have an associated URI scheme. See https://github.com/apache/cordova-plugin-file#append-a-file-using-alternative-methods-

          • 2. Re: Is there an easier way of accessing files locally
            SubLock69 Level 1

            Thank you! That helps a lot, I should really get used to using this and understand how it all works. And yes, the examples do need some sort of callback, I just wasn't thinking about that at the time. Thanks again for the feedback, I will try those out and understand the API more!