Android : Dwonload PDF on Local path in Cordova Android (Intel XDK)

on Wednesday, August 6, 2014


I am trying to read pdf . Now when I run my intel XDK code on App preview from local or server side I never get the pdf to download. It always shows errors. Now I then checked my same code through building my app and running on android here it is also not working. After this I then checked my code through debug tab in Intel XDK . At that moment I was able to see the pdf to be download and viewed it on pdf reader. Now app should not be always run from debug tab it always be build and uploaded on some app store. So I am unable to understand that why I am not getting my pdf download from after build. I also built my code through Cordova and now I am using Intel XDK version 1199. I also give all permission and included file and file transfer plugin. Despite all I have no sccess.


HTML:



<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta charset="utf-8">
<link href="styles/main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" charset="utf-8" src="intelxdk.js"></script>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8" src="scripts/main1.js"></script>
</head>
<body>
<header class="header">
</header>
<div class="content">
<div>
<button class="button big" id="OpenFiles">Open File</button>
</div>
<div id="infoField" type="text" readonly="readonly"></div>
</div>
<footer class="footer">
<div>Cordova API Samples</div>
</footer>
</body>
</html>


JS:



document.addEventListener("deviceready", onDeviceReady, false);
//Activate :active state
document.addEventListener("touchstart", function() {
}, false);

function onDeviceReady() {

navigator.splashscreen.hide();

document.getElementById('OpenFiles').onclick = function() {
var app = new Application();
app.Run();
}
}

function Application() {
}

Application.prototype.Run = function() {


var that = this;
that.getFilesystem(
function(fileSystem) {
alert("success");
that.transferFile(fileSystem.root.fullPath);
},
function() {
alert("failed to get filesystem");
}
);
}

Application.prototype.getFilesystem = function(success, fail) {
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, success, fail);
}

Application.prototype.transferFile = function(fileSystemPath) {
var transfer = new FileTransfer(),
uri = encodeURI("http://www.ancestralauthor.com/download/sample.pdf");
var filename = uri.substr(uri.lastIndexOf("/") + 1, uri.length);
alert(fileSystemPath)
filePath = fileSystemPath + '/' + filename;
transfer.download(
uri,
filePath,
function(entry) {

if (device.platform === 'Android') {
var path = entry.fullPath;

window.open(path, '_system');
}
else {
var url = entry.fullPath;

window.open(url, '_blank');
}
},
function(error) {

alert("download error source " + error.source);
alert("download error target " + error.target);
alert("upload error code" + error.code);
}
)
}

0 comments:

Post a Comment