文件读取器无法在PhoneGap Windows中工作
File reader not working PhoneGap Windows
我正在使用cordova在Visual Studio 2012上开发一个Windows应用程序。我试着创建了一个.txt文件,然后在阅读了该文件的内容后。
这就是我尝试过的,
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
function gotFS(fileSystem) {
// alert("File created");
fileSystem.root.getFile("textfile.txt", {create: true, exclusive: false}, gotFileEntry, fail);
}
function gotFileEntry(fileEntry) {
// alert(fileEntry.fullPath);
fileEntry.createWriter(gotFileWriter, fail);
}
function gotFileWriter(writer) {
/*writer.onwrite = function(evt) {
fileRead();
alert("write success");
console.log("write success");
}; */
/* writer.onerror = function(evt) {
alert("Writing abort");
}; */
/* writer.onprogress = function(evt) {
alert("Progress writing file");
}; */
writer.onwritestart = function(parents) {
// alert("Starts" + parents);
}
writer.onwriteend = function(parent) {
alert("Success or Fail "+ parent.target);
};
writer.write("some sample text");
// alert("Writing Data" + writer);
// contents of file now 'some sample text'
writer.truncate(11);
// contents of file now 'some sample'
writer.seek(4);
// contents of file still 'some sample' but file pointer is after the 'e' in 'some'
writer.write(" different text");
// contents of file now 'some different text'
}
function fileRead(){
//alert("inside file read");
fileSystem.root.getFile('textfile.txt', {create: false, exclusive: false}, gotFileEntryForRead, fail);
}
function gotFileEntryForRead(fileEntry) {
alert(fileEntry);
//alert("inside got file entry for read");
fileEntry.file(gotFile, fail);
}
function gotFile(file){
//readDataUrl(file);
readAsText(file);
}
function readDataUrl(file) {
var reader = new FileReader();
reader.onloadend = function(evt) {
console.log("Read as data URL");
console.log(evt.target.result);
};
reader.readAsDataURL(file);
}
function readAsText(file) {
//alert("inside readAsText");
var reader = new FileReader();
reader.onloadend = function(evt) {
if(evt.target.result == null) {
alert("File doesn't exists");
} else {
alert("File exists");
}
alert(evt.target.result);
console.log("Read as text");
console.log(evt.target.result);
};
reader.readAsText(file);
}
function fail(evt) {
alert("Fail");
//alert("Error:" + evt.target.error.code);
//console.log(evt.target.error.code);
}
该文件是在调用属性onwrite
时创建的,但当我尝试使用警报alert(evt.target.result);
查看文件的内容时,什么都没有发生。
请帮助
感谢
您在gotFileEntry、onDeviceReady和gotFS函数中使用了fail函数。这些函数执行requestFileSystem、getFile和writeFile功能。你将如何调试错误所在。因此,编写它们单独的失败处理程序函数可以轻松地处理错误,然后修复错误。在这种情况下,你不会准确地得到错误。
其次检查您是否在cordova文件中添加了权限。
提示:有些时候模拟器不能像真实设备那样提供完整的功能。也可以在真实设备中测试应用程序。
相关文章:
- Windows phone 8和phonegap 2.7.0,HTML页面和Xaml之间的导航以及返回
- 在PhoneGap Build上构建Windows Phone 7应用程序
- 将字符串从 C# 传递到 Javascript with Phonegap 和 Windows Phone 8
- 如何在PhoneGap Windows Phone中从JavaScript调用C#代码
- 跨域资源共享(CROS)似乎不适用于PhoneGap中的Windows Phone 8
- Windows 7 上 Android 的 Phonegap 3.0 构建错误
- Windows Phone 8&Phonegap:如何使用后退按钮关闭应用程序
- 本地存储在Windows手机PhoneGap应用程序中不起作用
- 文件读取器无法在PhoneGap Windows中工作
- PhoneGap 3.6.3 错误安装在 Windows 中
- Windows Phone PhoneGap应用返回按钮问题
- Phonegap中的多屏幕支持和android、iphone、windows黑莓的图像大小
- 无法在使用Phonegap的基于JavaScript的Windows 8应用程序中使用SQLite
- 在Windows RT上用phonegap上传mp3文件[Unicode映射错误]
- 如何在PhoneGap中实现windows平台的地图(Google或Bing)
- 如何调试phonegap windows phone 8.1
- Phonegap Windows phone 8自定义网页字体
- PhoneGap Windows Phone 8 IndexedDB AbortError when opening d
- PhoneGap Windows Phone 8-从www/文件夹访问资源
- 不能在PhoneGap Windows Phone 8中加载图像到HTML5画布