【问题标题】:Creating a .txt file in phonegap 2.0.0在 phonegap 2.0.0 中创建一个 .txt 文件
【发布时间】:2013-01-18 09:17:39
【问题描述】:

我正在尝试向现有的 PhoneGap Build(通过 Cordova 2.0.0)应用程序添加一个功能。

该应用已有许多功能,这应该是一个简单的mod。

我想要做的就是用 Cordova 2.0.0 FileWriter API(我认为)编写一个简单的 HTML 页面,将笔记保存为 .txt 文件(或者它可以是 .html 文件)然后可以稍后上传到PC并将文本内容复制到word文档/电子邮件等......

我已经在http://docs.phonegap.com/en/2.0.0/cordova_file_file.md.html#FileWriter 开始使用 Apache Cordova API,但是我很难理解这一点。#

我也看过Can't get Phonegaps FileWriter to work,但是这看起来甚至没有得到解决...

实际上这是我正在尝试使用的 HTML 文件...

<form action="" method="post">
    <br>
    <input style="width:100%" class="center" type="text" name="filename" placeholder="Input File Name Here"><br>
    <textarea name="notes" class="center" style="width:100%" placeholder="Please Type Notes Here"></textarea><br>
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td><input style="width:100%" class="button-big" type="submit" name="save2" value="save"></td>
        <td><input style="width:100%" class="button-big" type="reset" name="clear2" value="clear"></td>
      </tr>
      </table>
    <br>
    </form>

它是一个简单的 HTML 表单,但我想要一些 JS 或其他东西来使保存按钮使用文件名+预定义的文件扩展名(.txt 或 .htm 或 .html)将文件保存到根目录

如果有人能帮忙,那就太好了

谢谢,

亨利

【问题讨论】:

    标签: html cordova forms cordova-2.0.0


    【解决方案1】:
    // create a file writer object
    function CreateFileWriter()
    {
        // request the file system object
        window.requestFileSystem( LocalFileSystem.PERSISTENT, 0, OnFileSystemSuccess,fail);
    }
    
    function OnFileSystemSuccess( pFileSystemObj )
    {
        console.log( pFileSystemObj.name );
        console.log( pFileSystemObj.root.name );
    
        var file_name = document.getElementById('filename').value;
    
        pFileSystemObj.root.getFile( file_name, {create: true, exclusive: false}, OnFileGetSuccess, fail);
    }
    
    function OnFileGetSuccess( pFileEntryObj )
    {
        pFileEntryObj.createWriter( function(pWriterObj){ 
        gWriterObj  = pWriterObj; 
        }, fail );
    }
    
    function fail(evt)
    {
        console.log(evt.target.error.code);
    }
    

    希望有帮助。

    【讨论】:

    • 这会使用表单域来创建文件名吗?我需要从&lt;input style="width:100%" class="center" type="text" name="filename" placeholder="Input File Name Here"&gt; 中提取“file_name.txt”这可能吗?谢谢,亨利
    • 我已经编辑了我的答案,现在它会选择您输入控件的值。只需为您的控件提供“id”属性,就像您提供了 name 属性一样。这应该对您有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 2020-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多