How to store and retrieve file in iphone and android in jquery mobile

Thread is closed for posting
8 posts, 0 answers
  1. Deepan
    Deepan avatar
    19 posts
    Member since:
    Oct 2013

    Posted 12 Dec 2013 Link to this post

    Now i am using below file concept in  jquery mobile, this working fine only in android device not in iphone

    is any possible to store file inside our App not in sdcard (android and iphone)?


     /* Create Local Xml Files */
        function store_LocalXML() {
            window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
        }
     
        function gotFS(fileSystem) {
            
            // create dir
            fileSystem.root.getDirectory("folderName", {
                create: true,
                exclusive: false
            }, gotDirEntry, fail);
            
        }

        function gotDirEntry(dirEntry) {
            // create file
            dirEntry.getFile("filename", {
                create: true,
                exclusive: false
            }, gotFileEntry, fail);
            
        }

        function gotFileEntry(fileEntry) {
            fileEntry.createWriter(gotFileWriter, fail);
            
        }

        function gotFileWriter(writer) {
            writer.onwrite = function (evt) {
               // alert("write completed");
            };
            writer.write(stored_Content);
            writer.abort();
        }

        function fail(error) {
            alert(error.code);
        }
  2. David Silveria
    David Silveria avatar
    234 posts
    Member since:
    May 2006

    Posted 16 Dec 2013 Link to this post

    Do you get any errors on iOS, have you tried debugging the code to see what goes wrong? The File sample works on both Android and iOS, so I would check for discrepancies.
  3. Deepan
    Deepan avatar
    19 posts
    Member since:
    Oct 2013

    Posted 18 Dec 2013 Link to this post

    "File Sample" coding is working well , but i need directory to store the files. So i used this type of coding, 
    in "File Sample" coding datas are appending in files, i need data should be overwrite or file should be overwrite .
     
    In my coding
    "writer.write(stored_Content);"
    this line is working fine to create a file at a first time("test.xml"), but this line is not working  after deleted this file and recreate a same file name("test.xml") .

    i wish to delete and create same filename "test.xml" at same time.


    function delete_localXML() {
            window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,     delete_process, fail);

        }
        
        function delete_process(fileSystem) {
            // create dir
            fileSystem.root.getDirectory("HEMS", {
                create: true,
                exclusive: false
            }, gotDirEntry_delete, fail);
        }
        
        function gotDirEntry_delete(dirEntry) {
            // create file
            dirEntry.getFile(delete_FileName, {
                create: true,
                exclusive: false
            }, deleteFile, fail);
        }
        
        function deleteFile(fileEntry) {
            fileEntry.remove(function() {
               console.log("File Removed");
            });
        }

  4. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 21 Dec 2013 Link to this post

    Hi Deepan,

    I believe the file deletion and creation in your code snippet are mixed up. The steps for replacing a file should be as follows:
    1. Get directory (success/fail);
    2. Get file (success/fail);
    3. Delete file (success/fail);
    4. Create file (success/fail).

    I reworked your code like this:

    function delete_process(fileSystem) {
        // You should get the directory or create it if it doesn't exist
        // and on success call the gotDirEntry fucntion.
        fileSystem.root.getDirectory("HEMS", {
            create: true,
            exclusive: false
        }, gotDirEntry, fail);
    }
     
    function gotDirEntry(dirEntry) {
        // You do not need to create the file as it should already exist in the system.
        // You should only get the file and on success call the deleteFile function.
        // On fail you should directly create the new file.
        dirEntry.getFile(delete_FileName, {
            create: false,
            exclusive: false
        }, deleteFile, CreateDirEntry);
    }
     
    function deleteFile(fileEntry) {
        // First we delete the file and on success we call the CreateDirEntry function.
        fileEntry.remove(CreateDirEntry, fail);
    }
     
    function CreateDirEntry(dirEntry) {
        // Finally you need to create the new file again in the according dir.
        dirEntry.getFile(delete_FileName, {
            create: true,
            exclusive: false
        }, success, fail);
    }


    Further, you can check the Cordova File Api documentation where you will find more information and some good usage examples about the api.

    I hope this helps.
    Regards,
    Kaloyan
    Telerik
    You've missed the Icenium Visual Studio Integration keynote? It has been recorded and posted here.
    Looking for tips & tricks directly from the Icenium team? Check out our blog!
    Share feedback and vote for features on our Feedback Portal.
  5. Deepan
    Deepan avatar
    19 posts
    Member since:
    Oct 2013

    Posted 22 Dec 2013 Link to this post

    Thank u for your response
    Your code is perfectly working only in Android. but same error occur in IPhone. file creation not worked after delete functionality.

    i got solution for this,

    // test.xml
    delete_localXML();


    // check file availability using ajax

     $.ajax({
                type: 'GET',
                url: filePath,
                dataType: 'html',
                success: function (data) {
             // File Available     
                },
                error: function (e) {
                    // File not Available

                  // test.xml
                   store_LocalXML();
                },
            });

    this coding working perfectly in Iphone and also Android, what i expected
  6. Iva Koevska
    Admin
    Iva Koevska avatar
    286 posts

    Posted 24 Dec 2013 Link to this post

    Hi Deepan,

    Thank you for sharing this solution.

    Regards,
    Iva Koevska
    Telerik
    You've missed the Icenium Visual Studio Integration keynote? It has been recorded and posted here.
    Looking for tips & tricks directly from the Icenium team? Check out our blog!
    Share feedback and vote for features on our Feedback Portal.
  7. Deepan
    Deepan avatar
    19 posts
    Member since:
    Oct 2013

    Posted 28 Jan 2014 Link to this post

    Hi
      Am getting issue in Deleting and overwriting file concept in Iphone

    One time delete method is working, for second time this is not working properly.

     While deleting
     fileEntry.remove(function() {
               console.log("File Removed");
            });

    this method is not working in iphone .
    Am not getting any error ,in log am getting "File Removed",  but files are not deleting .
  8. Steve
    Admin
    Steve avatar
    10941 posts

    Posted 30 Jan 2014 Link to this post

    Hi Deepan,

    You can try debugging directly on the device to figure out what goes wrong: Debug on iOS Device.

    Regards,
    Steve
    Telerik
    Icenium is now Telerik AppBuilder, and is part of the Telerik Platform. For more information on the new name, and to learn more about the Platform, register for the free online keynote and webinar on Wednesday, February 12, 2014 at 11:00 a.m. ET (8:00 a.m. PT)
Back to Top