在Phonegap iOS应用程序中查看/下载文件
问题描述:
Im构建iOS phonegap(v 1.3)应用程序。在这个页面上,我给了一个按钮来下载一个.pptx文件。在Phonegap iOS应用程序中查看/下载文件
<a href="'+downloadUrl+'" target="_blank">Download file</a>
按钮的href指向我网站上的下载链接。所以点击链接应该会自动下载文件。
发生什么事是,点击链接时,文件被下载,但它在相同的视图中打开。 pptx的所有幻灯片都会一个在另一个下方,并占据整个屏幕。没有办法回到我的应用程序,除了杀死我的应用程序并重新启动。我尝试了target =“_blank”,“_tab”,没有任何工作,所有的结果都与屏幕上的幻灯片有类似的行为,并且没有办法让我回到我的应用程序。
我可以做些什么,让.pptx文件在另一个视图中打开,或者更好,根本不打开,只是得到保存(像在android中)?请帮助。
答
,下载和显示文件,请按照示例代码。
包含给定的代码只是</head>
标签上面您的index.html
<script type="text/javascript" charset="utf-8">
// Wait for Cordova to load
document.addEventListener("deviceready", onDeviceReady, false);
// Cordova is ready
function onDeviceReady() {
alert("Going to start download");
downloadFile();
}
function downloadFile(){
window.requestFileSystem(
LocalFileSystem.PERSISTENT, 0,
function onFileSystemSuccess(fileSystem) {
fileSystem.root.getFile(
"dummy.html", {create: true, exclusive: false},
function gotFileEntry(fileEntry){
var sPath = fileEntry.fullPath.replace("dummy.html","");
var fileTransfer = new FileTransfer();
fileEntry.remove();
fileTransfer.download(
"http://www.w3.org/2011/web-apps-ws/papers/Nitobi.pdf",
sPath + "theFile.pdf",
function(theFile) {
console.log("download complete: " + theFile.toURI());
showLink(theFile.toURI());
},
function(error) {
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code: " + error.code);
}
);
},
fail);
},
fail);
}
function showLink(url){
alert(url);
var divEl = document.getElementById("deviceready");
var aElem = document.createElement("a");
aElem.setAttribute("target", "_blank");
aElem.setAttribute("href", url);
aElem.appendChild(document.createTextNode("Ready! Click To Open."))
divEl.appendChild(aElem);
}
function fail(evt) {
console.log(evt.target.error.code);
}
</script>
参见: - Blog Post