最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Phonegap - Creating a .txt file on first load - Stack Overflow

programmeradmin1浏览0评论

I am creating a phonegap app and need to create a new .txt file on first load. After this I need to check whether the file exists and then ignore the creation if that is the case, below is the general flow that I am after:

1 - onDeviceReady - loading phoengap app 2 - Check is readme.txt exist (if yes load home page) 3 - Create a file readme.txt and add to www folder 4 - Continue loading homepage

EDIT - Rather than the valid answer mentioned below I decided to use HTML5s local storage as this was simply 1 line of code.

localStorage.setItem("name", "Email Supplied!");

and can be checked using this simple if statement

 if (localStorage.getItem("name") == "Email Supplied!")
        {
            // What you want to happen here
        }

I am creating a phonegap app and need to create a new .txt file on first load. After this I need to check whether the file exists and then ignore the creation if that is the case, below is the general flow that I am after:

1 - onDeviceReady - loading phoengap app 2 - Check is readme.txt exist (if yes load home page) 3 - Create a file readme.txt and add to www folder 4 - Continue loading homepage

EDIT - Rather than the valid answer mentioned below I decided to use HTML5s local storage as this was simply 1 line of code.

localStorage.setItem("name", "Email Supplied!");

and can be checked using this simple if statement

 if (localStorage.getItem("name") == "Email Supplied!")
        {
            // What you want to happen here
        }
Share edited Sep 24, 2013 at 9:13 SingleWave Games asked Feb 27, 2012 at 9:28 SingleWave GamesSingleWave Games 2,6589 gold badges38 silver badges53 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 1

You can take a look at the full example here:

http://docs.phonegap./en/1.4.1/phonegap_file_file.md.html#FileWriter

This line create the file if it doesn't exist :

fileSystem.root.getFile("readme.txt", {create: true, exclusive: false}, gotFileEntry, fail);

Supported Platforms

Android BlackBerry WebWorks (OS 5.0 and higher) iOS Windows Phone 7 ( Mango )

I don't know about there others but in iOS, the document is created in /var/mobile/Application/YOU_APP/Documents

[CODE]

        <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, gotFS, fail);
            }

            function gotFS(fileSystem) {
                fileSystem.root.getFile("readme.txt", {create: true}, gotFileEntry, fail);
            }

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

            function gotFileWriter(writer) {
                writer.onwrite = function(evt) {
                    console.log("write success");
                };

                writer.write("some sample text");
                writer.abort();
                // contents of file now 'some different text'
            }

            function fail(error) {
                console.log("error : "+error.code);
            }

        </script>

Hope it helps

Depending on the devices you are developing for you can look at using their native file creation APIs. For example, iOS uses plists. Android does use .txt files, look at this link for more information.

发布评论

评论列表(0)

  1. 暂无评论