0
我正在用cordova开发Visual Studio 2012上的Windows应用程序。我试图创建,然后读取.txt文件的内容。文件读取器不能正常工作PhoneGap Windows
这是我试过,
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);
然后什么都没看到文件的内容发梗。
请帮
感谢
谢谢。正如你所说,我正在使用真正的设备进行测试,而对于Windows Phone,这项工作没有许可。 – shiv 2014-10-10 11:11:55