【发布时间】:2018-09-21 14:29:17
【问题描述】:
我正在学习如何使用 phonegap 读取文件。我从头开始一切。这就是我所做的:
- 下载并安装 npm
- 通过 npm 安装 Phonegap (npm install -g phonegap)
- 通过 npm (npm install -g cordova) 安装 Cordova,这可能是不必要的,但我还是这样做了
- 创建我的测试应用(phonegap create testfile)
- 添加android平台(phonegap平台添加android)
之后我注意到我的插件文件夹和我的 config.xml 中已经包含了 cordova-plugin-file,所以我没有安装它(如果我错了,请纠正我)
然后,在 index.html 文件的底部,我像这样编辑它:
.
.
.
<script type="text/javascript">
app.initialize();
</script>
<script>
function startFile()
{
window.resolveLocalFileSystemURL('file:///storage/emulated/0/newfile.txt', readFile, fail);
}
function readFile(fileEntry)
{
fileEntry.file(function(file)
{
var reader = new FileReader();
reader.onloadend = function(e)
{
var content = this.result;
alert("file content: " + content);
};
reader.readAsText(file);
});
}
function fail(e)
{
alert(e.code);
}
</script>
<input type="button" value="read file" onclick="startFile()" />
</body>
</html>
非常简单,因为我只想看看它如何读取文件并在警报消息框中查看它。
我已经将 newfile.txt 复制到我的 android 存储上,它似乎找到了该文件,但警报显示“文件内容:空”
文件的内容就是“hello world”。有人可以帮我解决这个问题吗?
【问题讨论】:
标签: javascript cordova phonegap