我從此鏈接下面的代碼http://weblogs.asp.net/soever/cordova-file-transfer-unzip-and-present-adventures根據我的需要更改了URL。點擊「加載」按鈕沒有觸發事件,LocalFileSystem也沒有定義
<!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>
因爲我想實現這樣的傢伙在他的職位,下載一個ZIP文件,然後解壓到本地沙盒文件系統。當我使用「多設備混合應用程序」+ Cordova擴展在Visual Studio 2013中複製並粘貼此代碼並嘗試使用Ripple-Nexus7(平板電腦)運行它時,它帶來了一個不錯的用戶界面,但是當我單擊「加載」第一次在下面的一段代碼中給我錯誤。
getFilesystem:function (success, fail) {
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, success, fail);
},
「LocalFileSystem」沒有定義,那麼我想補充文件,從內VS2013 config.xml中的插件選項卡中文件傳輸的插件,但它無法取得使用Plugman這些插件。最後,我從GitHub下載了這些軟件包,並在解決方案資源管理器中的項目中添加了一個plug_ins文件夾,並將這些插件放入該plug_ins文件夾中,保持其內部文件和文件夾結構不變。現在它不再給我這個錯誤了(儘管我還沒有能夠在intellisense中找到LocalFileSystem對象,這意味着一些奇怪的原因,它不會給我同樣的LocalFileSystem未定義的錯誤,但仍然這些插件不工作)。
現在,當我運行代碼和UI出現後,我點擊「加載」按鈕什麼也沒有發生,在此之前,「OnDeviceReady」函數應該在「deviceready」事件中調用,但沒有任何事情發生在那裏。
請大家在這方面我需要很多幫助,因爲我對JavaScript和科爾多瓦的東西很陌生。 我會衷心感謝所有能夠幫助我解決這些問題的人。
我把我的筆記本電腦帶到不同的位置,我可以通過使用config.xml的插件選項卡下載Cordova擴展,並構建成功。但它仍然給我同樣的錯誤LocalFileSystem未定義。現在我完全掌握了什麼是錯的想法。 – user2913184 2014-10-30 01:20:34
我真的很感激任何意見/建議。 – user2913184 2014-10-31 16:30:50
你有沒有試過用仿真器代替紋波運行你的代碼? Chrome瀏覽器默認不啓用文件系統訪問權限。要解決這個問題,請按照第一條評論[這裏]中的步驟操作(http://www.tricedesigns.com/2012/07/31/emulating-phonegapcordova-apps-in-the-browser/) – Avani 2014-10-31 18:18:04