【问题标题】:Read Write text file using HTML code in Titanium Appcelerator在 Titanium Appcelerator 中使用 HTML 代码读取写入文本文件
【发布时间】:2023-03-22 00:17:01
【问题描述】:

我正在 Titanium Appcelerator 中创建 HTML5 类型的应用程序。我编写了代码以使用正确执行的钛代码创建文本文件,并在/Users/demoUser/Library/Developer/CoreSimulator/Devices/FE1CF0AC-D5BD-4FAB-9615-C58D80B5A9C6/data/Containers/Data/Application/40686DB0-BFB0-4D01-98BB-9E5758C4976D/Documents/file.txt创建文本文件

现在我有一个 html 文件,即 index.html,我在同一个应用程序的钛 webview 中加载它。现在我想在 .html 文件中创建的函数中访问 file.txt 的内容。

有谁曾经做过这样的事情吗?或者对此提供任何帮助或建议将不胜感激。

【问题讨论】:

    标签: javascript html titanium titanium-mobile html5-apps


    【解决方案1】:

    您可以从资源目录或应用程序目录中读取文件并将其呈现在 html 页面中,如下所示。

     var readContents;
    

    var readFile = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'data.txt');

    if (readFile.exists()) { readContents = readFile.read(); Ti.API.info('File Exists');
    }

    var docString = readContents.toString(); Ti.API.info('Contents = ' + docString);

    var text_in_html = "<html><body><pre>" + docString + "</pre></body></html>";

    // Create our Webview var myWV = Ti.UI.createWebView({ html:text_in_html, title:'Title goes here', left:0, right:0, top:0, bottom:0, loading: true
    });

    【讨论】:

    • 感谢您的宝贵意见。但我想要的是我想从 html 文件本身读取位于 applicationDataDirectory 中的文件内容。即我想在 index.html 本身中创建直接从 applicationDataDirectory 读取内容的函数。
    猜你喜欢
    • 1970-01-01
    • 2011-03-08
    • 1970-01-01
    • 1970-01-01
    • 2014-06-29
    • 1970-01-01
    • 1970-01-01
    • 2016-10-06
    • 1970-01-01
    相关资源
    最近更新 更多