SIGN UP MEMBER LOGIN:    
ARTICLE

Using Flags and LocalFileSystem in PhoneGap

Posted by Sanjoli Gupta Articles | Windows Phone in C# February 10, 2012
In this article, we will learn the use of Flags and LocalFileSystem in PhoneGap.
Reader Level:

The LocalFileSystem object is used to supply arguments to DirectoryEntry objects such as the getFile and getDirectory methods, which you can use to look up or create files and directories, respectively.

The following are the properties.

  • create : This is used to specify that a file or directory should be created, if it does not exist.
  • exclusive : This is used with create. The exclusive flag causes the file or directory creation to fail if the target path already exists.

The following is an Example:

      // Get the data directory, creating it if it doesn't exist.
     dataDirect = fileSystem.root.getDirectory("data", {create: true});
    // Create the status file, if and only if it doesn't exist.
    statusFile = dataDir.getFile("statusfile.txt", { create: true, exclusive: true });

Using LocalFileSystem

This object provides a way to obtain root filesystems. It has two methods:

  • requestFileSystem : This method is used to request a fileSystem.

  • resolveLocalFileSystemURI : This method is used to retrieve a DirctoryEntry or FileEntry object using a local URI.

Two constants define a local filesystem. 

  • LocalFileSystem.PERSISTENT : This is used for the storage that is not deleted by any user agent without permission from the application or user.

  • LocalFileSystem.TEMPORARY : This is used for storage with no guarantee of persistence.

The following is an example that requests the local filesystem:

function onSuccess(fileSystem)
{
       console.log(fileSystem.name);
}
// request the persistent file system
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onSuccess, onError);

Here's an example that resolves the local system URI:

function onSuccess(fileEntry)
{
     console.log(fileEntry.name);
}
window.resolveLocalFileSystemURI("File:///new.txt", onSuccess, onError);

Full Example

<!DOCTYPE html>
<html>
  <head>
    <title>Local_File_System Example</title>

    <script type="text/javascript" charset="utf-8" src="phonegap-1.3.0.js"></script>
    <script type="text/javascript" charset="utf-8">

        // Wait for PhoneGap to load
        //
        document.addEventListener("deviceready", onDeviceReady, false);

        // PhoneGap is ready
        //
        function onDeviceReady() {
            window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSuccess, fail);
            window.resolveLocalFileSystemURI("file://new.txt", onResolveSuccess,fail );
        }

        function onFileSuccess(fileSystem) {
            console.log(fileSystem.name);
        }

        function onResolveSuccess(fileEntry) {
            console.log(fileEntry.name);
        }

Resources

Some of the useful resources are as follows:

Working With Networks in Windows Phone 7 Using PhoneGap
Media in PhoneGap
Events in PhoneGap

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor