Javascript 单击“加载”按钮不会触发事件,并且未定义LocalFileSystem
我有以下代码从这个链接改变只是根据我的需要网址Javascript 单击“加载”按钮不会触发事件,并且未定义LocalFileSystem,javascript,visual-studio,cordova,multi-device-hybrid-apps,Javascript,Visual Studio,Cordova,Multi Device Hybrid Apps,我有以下代码从这个链接改变只是根据我的需要网址 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>DemoZipUnzip</title> <meta http-equiv="Content-type" content="text/html; charset=utf-8"> <meta name="viewport" content=
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>DemoZipUnzip</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
<!-- DemoZipUnzip references -->
<link href="css/index.css" rel="stylesheet" />
<!-- Cordova reference, this is added to your app when it's built. -->
<!--<script src="cordova.js"></script>-->
<script src="scripts/platformOverrides.js"></script>
<script src="scripts/index.js"></script>
</head>
<body>
<button id="btnLoad">Load</button>
<button id="btnUnzip">Unzip</button>
<hr />
<div id="statusPlace"></div>
<hr />
<img id="imgPlace" src="http://lorempixel.com/320/200">
<br />
<div id="txtPlace">TEXT COMES HERE</div>
<script type="text/javascript">
document.addEventListener("deviceready", onDeviceReady, false);
function registerHandlers() {
document.getElementById("btnLoad").onclick = function () {
var that = this,
App = new DownloadApp(),
fileName = "Archive.zip",
uri = "http://localhost:63961/check/contentupdate/availableupdates",
folderName = "content";
console.log("load button clicked");
document.getElementById("statusPlace").innerHTML += "<br/>Loading: " + uri;
App.load(uri, folderName, fileName,
/*progress*/function (percentage) { document.getElementById("statusPlace").innerHTML += "<br/>" + percentage + "%"; },
/*success*/function (entry) { document.getElementById("statusPlace").innerHTML += "<br/>Zip saved to: " + entry.toURL(); },
/*fail*/function () { document.getElementById("statusPlace").innerHTML += "<br/>Failed load zip: " + that.uri; }
);
};
document.getElementById("btnUnzip").onclick = function () {
var that = this,
App = new DownloadApp(),
fileName = "Archive.zip",
folderName = "content";
console.log("zip button clicked");
App.unzip(folderName, fileName,
/*success*/function () { alert("Unzipped and assigned"); },
/*fail*/function (error) { alert("Unzip failed: " + error.code); }
);
};
}
function onDeviceReady() {
// navigator.splashscreen.hide();
document.getElementById("statusPlace").innerHTML += "<br/>deviceready event received";
registerHandlers();
}
var DownloadApp = function () {
}
DownloadApp.prototype = {
load: function (uri, folderName, fileName, progress, success, fail) {
var that = this;
that.progress = progress;
that.success = success;
that.fail = fail;
filePath = "";
that.getFilesystem(
function (fileSystem) {
console.log("GotFS");
that.getFolder(fileSystem, folderName, function (folder) {
filePath = folder.toURL() + "/" + fileName;
that.transferFile(uri, filePath, progress, success, fail);
}, function (error) {
console.log("Failed to get folder: " + error.code);
typeof that.fail === 'function' && that.fail(error);
});
},
function (error) {
console.log("Failed to get filesystem: " + error.code);
typeof that.fail === 'function' && that.fail(error);
}
);
},
getFilesystem:function (success, fail) {
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, success, fail);
},
getFolder: function (fileSystem, folderName, success, fail) {
fileSystem.root.getDirectory(folderName, { create: true, exclusive: false }, success, fail)
},
transferFile: function (uri, filePath, progress, success, fail) {
var that = this;
that.progress = progress;
that.success = success;
that.fail = fail;
var transfer = new FileTransfer();
transfer.onprogress = function (progressEvent) {
if (progressEvent.lengthComputable) {
var perc = Math.floor(progressEvent.loaded / progressEvent.total * 100);
typeof that.progress === 'function' && that.progress(perc); // progression on scale 0..100 (percentage) as number
} else {
}
};
transfer.download(
uri,
filePath,
function (entry) {
console.log("File saved to: " + entry.toURL());
typeof that.success === 'function' && that.success(entry);
},
function (error) {
console.log("An error has occurred: Code = " + error.code);
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("download error code " + error.code);
typeof that.fail === 'function' && that.fail(error);
}
);
},
unzip: function (folderName, fileName, success, fail) {
var that = this;
that.success = success;
that.fail = fail;
zip.unzip("cdvfile://localhost/persistent/" + folderName + "/" + fileName,
"cdvfile://localhost/persistent/" + folderName,
function (code) {
console.log("result: " + code);
that.getFilesystem(
function (fileSystem) {
console.log("gotFS");
that.getFolder(fileSystem, folderName + "/ftpack", function (folder) {
document.getElementById("imgPlace").src = folder.nativeURL + "/img.jpg";
folder.getFile("text.html", { create: false }, function (fileEntry) {
fileEntry.file(function (file) {
var reader = new FileReader();
reader.onloadend = function (evt) {
console.log("Read as text");
console.log(evt.target.result);
document.getElementById("txtPlace").innerHTML = evt.target.result;
typeof that.success === ' function && that.success();'
};
reader.readAsText(file);
}, function (error) {
console.log("Failed to get file");
typeof that.fail === 'function' && that.fail(error);
});
}, function (error) {
console.log("failed to get file: " + error.code);
typeof that.fail === 'function' && that.fail(error);
});
}, function (error) {
console.log("failed to get folder: " + error.code);
typeof that.fail === 'function' && that.fail(error);
});
}, function (error) {
console.log("failed to get filesystem: " + error.code);
typeof that.fail === 'function' && that.fail(error);
});
}
);
}
}
</script>
LocalFileSystem没有定义,然后我尝试在VS2013中从config.xml的插件选项卡添加文件、文件传输插件,但使用Plugman无法获取这些插件。最终,我从GitHub下载了这些包,并从解决方案资源管理器在我的项目中添加了一个plug_-ins文件夹,并将这些插件放入该plug_-ins文件夹中,保持其内部文件和文件夹结构不变。现在不再出现该错误,尽管我仍然无法在intellisense中找到LocalFileSystem对象,这意味着出于某种奇怪的原因,它没有返回相同的LocalFileSystem未定义错误,但这些插件仍然无法工作
现在,当我运行代码时,当我单击“加载”按钮时,UI出现,没有任何事情发生,绝对没有任何事情,在此之前,OnDeviceReady函数应该在deviceready事件中被调用,但那里也没有任何事情发生
伙计们,在这方面我需要很多帮助,因为我对Javascript和Cordova的东西非常陌生。
我将衷心感谢所有能够帮助我解决这些问题的人。是的,阿瓦尼,我已经发现ripple是罪魁祸首,很抱歉我没有更新,但我应该知道。它允许所有插件在运行时加载,但不允许它们初始化,这就是为什么我对所有未定义的对象都有问题。它是Visual Studio 2013的测试版,因此ripple存在相关问题。是的,仿真器也给了我同样的痛苦。我把笔记本电脑带到了另一个地方,通过使用config.xml的plug-in选项卡下载了Cordova extensions,并在构建成功后进行了构建。但它仍然给了我相同的错误LocalFileSystem未定义。现在我完全不知道怎么回事了。我真的非常感谢你的评论/建议。你试过用模拟器而不是ripple来运行你的代码吗?Chrome默认情况下不启用文件系统访问。要解决此问题,请按照第一个注释中的步骤进行操作。你能用你的答案更新这个问题吗?这样社区就能意识到了?谢谢我已经贴出了一个答案,上面有我之前的评论。
getFilesystem:function (success, fail) {
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, success, fail);
},