如何打开PhoneGap的应用程序下载的文件(PDF,ZIP,DOCX等)? [英] How to open downloaded file (pdf,zip,docx, etc) in Phonegap app?
问题描述
目前我正在下载模块。一旦用户点击下载按钮时,文件(或者以pdf,zip文件或DOCX)将被从服务器下载并存储在本地存储。我能够下载使用FileTransfer.Download()方法的文件,但我无法通过传递文件路径打开它。
Currently I am working on downloading module. Once user clicks download button, the file (either in pdf, zip file, or docx) will be downloaded from server and will be stored in local storage. I am able to download the file using FileTransfer.Download() method, but I can't open it by passing the file path.
下面是我的code:
<!DOCTYPE HTML>
<html>
<head>
<meta name = "viewport" content = "user-scalable=no,width=device-width" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Test Page</title>
<script type="text/javascript" charset="utf-8" src="js/cordova-2.0.0.js"></script>
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<style type="text/css">
* {
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}
</style>
<script type="text/javascript" charset="utf-8">
function init() {
document.addEventListener("deviceready", ready, true);
}
function ready() {
console.log("App is ready.");
}
function download() {
// var remoteFile = "https://dl.dropbox.com/u/6731723/a.zip";
var remoteFile = "https://dl.dropbox.com/u/6731723/Beaver.pdf";
var localFileName = remoteFile.substring(remoteFile.lastIndexOf('/') + 1);
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
fileSystem.root.getFile(localFileName, { create: true, exclusive: false }, function (fileEntry) {
var localPath = fileEntry.fullPath;
console.log("localPath1:" + localPath);
if (device.platform === "Android" && localPath.indexOf("file://") === 0) {
localPath = localPath.substring(7);
}
console.log("localPath2 save:" + localPath);
var ft = new FileTransfer();
ft.download(remoteFile,
localPath, function (entry) {
console.log("file path:" + entry.fullPath);
var linkopen = document.getElementById("openlink");
linkopen.style.display = "block";
linkopen.href = entry.fullPath;
// var dwnldImg = document.getElementById("dwnldImg");
//dwnldImg.src = entry.fullPath;
//dwnldImg.style.visibility = "visible";
//dwnldImg.style.display = "block";
}, fail);
}, fail);
}, fail);
}
function fail(error) {
console.log("error:" + error.code);
}
</script>
</head>
<body onload="init();">
<a href="#" onclick="download();">Download file</a>
<br />
<br />
<br />
<a href="#" id="openlink" style="display:none;font-size:larger;">open</a>
</body>
</html>
任何想法如何得到它的工作?
any idea how to get it work?
推荐答案
原生的PhoneGap不允许下载SD卡上的PDF文件,并不会自动显示PDF文件。
Natively PhoneGap does not allow to download a pdf file on your sd card and does not display PDF files automatically.
- 请在你的智能手机上安装PDF阅读器。
- 在下载文件创建一个插件。
- 用于打开使用安装在程序的PDF创建插件 第1步。
- Please install pdf reader on your smartphone.
- Create a plugin for downloading files.
- Create a plugin for opening the pdf using the program installed in step 1.
步骤2和3 的详细信息:
设置开发环境<一href="http://docs.phonegap.com/en/2.2.0/guide_getting-started_android_index.md.html#Getting%20Started%20with%20Android"相对=nofollow> PhoneGap的在Eclipse中。
从<一个下载的例子的java类href="https://docs.google.com/file/d/0B_TYS8wHcgB7MTJkMWY4MGQtM2JiYS00MzE3LThjMWQtMDFlNWI5YjM0N2Iw/edit?hl=en_US&pli=1"相对=nofollow>此处
解压缩的文件夹中下载的文件的src
从<一个例子下载的JavaScript href="https://docs.google.com/file/d/0B_TYS8wHcgB7N2I5N2I2OWMtM2I1Mi00NWYxLTk4NzMtM2JiNWIyMTJkZWU2/edit?hl=en_US&pli=1"相对=nofollow>此处
解压缩的文件夹中下载的文件资产 / WWW
在指数的html文件。给调用JS:
Details of Step 2 and 3:
Set the development environment PhoneGap in Eclipse.
Download the java classes of example from HERE
Unzip the downloaded file in the folder " src "
Download the javascript from the example from HERE
Unzip the downloaded file in the folder " assets / www "
In the Index html file. give calls to js:
<script charset="utf-8" src="main.js"> </script>
<script charset="utf-8" src="pdfviewer.js"> </script>
<script charset="utf-8" src="downloader.js"> </script>
在 RES / XML / plugins.xml 添加:
<plugin name="Downloader" value="com.phonegap.plugins.downloader.Downloader"/>
<plugin name="PdfViewer" value="com.phonegap.plugins.pdfViewer.PdfViewer"/>
要拨打以下code index.html中的插件定义的方法,测试:
To call the methods defined in the plugin, test following code in index.html:
<! DOCTYPE HTML>
<html>
<head>
<title> PhoneGap-Android PDF Opening App</title>
<script charset="utf-8" src="phonegap-1.0.0.js"></script>
<script charset="utf-8" src="main.js"></script>
<script charset="utf-8" src="pdfviewer.js"></script>
<script charset="utf-8" src="downloader.js"></script>
</Head>
<body>
<h1> Hello World </ h1>
<a href = "#" 'sampleurf.pdf', true, downloadOkCallbak, downloadErCallbak) "> Download pdf </a> <br>
<a href="#" onclick="window.plugins.pdfViewer.showPdf('/sdcard/download/sample/samplesurf.pdf');"> open </a>
</body>
</html>
幸得 HTTP:// WWW .giovesoft.com / 2011/08 /下载后打开,PDF与 - phonegap.html
这篇关于如何打开PhoneGap的应用程序下载的文件(PDF,ZIP,DOCX等)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!