Phonegap - 首次加载时创建.txt文件 [英] Phonegap - Creating a .txt file on first load

查看:190
本文介绍了Phonegap - 首次加载时创建.txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个phonegap应用程序,并需要在第一次加载时创建一个新的.txt文件。在这之后,我需要检查文件是否存在,然后忽略创建,如果是这样,下面是一般的流程,我之后:



1 - onDeviceReady - 加载phoengap应用程序
2 - 检查是readme.txt存在(如果是加载主页)
3 - 创建文件readme.txt并添加到www文件夹
4 - 继续加载首页

EDIT - 而不是下面提到的有效答案,我决定使用HTML5s本地存储,因为这只是一行代码。

  localStorage.setItem(name,Email Supplied!); 

,可以使用这个简单的if语句检查

  if(localStorage.getItem(name)==Email Supplied!)
{
//你想在这里发生什么
}


解决方案

这里:



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



此行创建文件if它不存在:

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





支持的平台


Android BlackBerry WebWorks(OS 5.0及更高版本)iOS Windows Phone 7(
Mango)


我不知道有其他人,但在iOS中,文档是在/ var / mobile / Application / YOU_APP / Documents中创建的



[CODE]

 < script type =text / javascriptcharset =utf-8> 

//等待PhoneGap加载
//
document.addEventListener(deviceready,onDeviceReady,false);

// PhoneGap就绪
//
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);
}

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

writer.write(some sample text);
writer.abort();
//文件的内容现在'一些不同的文本'
}

函数失败(错误){
console.log(error:+ error.code );
}

< / script>

希望它有帮助


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
        }

解决方案

You can take a look at the full example here:

http://docs.phonegap.com/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

这篇关于Phonegap - 首次加载时创建.txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆