PhoneGap中文网

 找回密码
 立即注册
查看: 12182|回复: 0
打印 上一主题 下一主题

Android版添加phonegap--文件下载插件教程

[复制链接]

5

主题

5

帖子

17

积分

新手上路

Rank: 1

积分
17
跳转到指定楼层
楼主
发表于 2015-12-29 11:53:07 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
插件的使用方法:
1.拷贝FileOpener.java到你的项目中

2.在插件文件里添加插件引用:
<plugin name="FileOpener" value="com.phonegap.plugins.fileopener.FileOpener"/>

3.拷贝fileopener.js到你的www文件下在你的html页面你们引入js文件。
<script type=”text/javascript” charset=”utf-8″ src=”fileopener.js”></script>

4.写下载代码:
在html页面你们添加:
<ul id=”pdf”>
<li><span><strong>文件</strong>< /span>
<span><strong>状态</strong>< /span>
<span><strong>查看</strong>< /span>
<span><strong>删除</strong></span>< /li>
<li><span><a href=”#”onClick=”openpdf(‘dropbox.pdf’)”>文件1</a></span>
<span><a href=”#”     onclick=”downloadFile(‘dropbox.pdf’,1);”>下载</a></span>
<span><a href=”#”    onClick=”openpdf(‘dropbox.pdf’)”>打开</a></span>
<span><a href=”#”     onClick=”removefile(‘dropbox.pdf’,1)”>删除</a></span>< /li>
<li><span><a href=”#”onClick=”openpdf(‘ad_pdf0.pdf’)”>文件2</a></span><span>
<a href=” #  ”      onclick=”downloadFile(‘ad_pdf0.pdf’,2);”>下载</a></span>
<span><a href=”# ”onClick=”openpdf(‘ad_pdf0.pdf’)”>打开</a></span>
<span><a href=”# ” onClick=”removefile(‘ad_pdf0.pdf’,2)”>删除</a></span>< /li>
<li><span ><a href=”#”onClick=”openpdf(‘purchase.pdf’)”>文件3</a></span>
<span& gt;<a href=” #” onclick=”downloadFile(‘purchase.pdf’,3);”>下载</a></span>
<span><a href=”#” onClick=”openpdf(‘purchase.pdf’)”>打开</a></span>
<span><a href=”#” onClick=”removefile(‘purchase.pdf’,3)”>删除</a></span>< /li>
</ul>

添加js方法:
1.初始化文件请求:
window.appRootDirName = “download_pdf”; //定义文件下载后的存放目录
document.addEventListener(“deviceready”, onDeviceReady, false);
function onDeviceReady() {
console.log(“device is ready”);
var gotFS=function(fileSystem) {
window.fileSystem = fileSystem; //将filesystem 定义为全局变量
fileSystem.root.getDirectory(window.appRootDirName, {//创建一个目录
create : true,
exclusive : false
}, dirReady, fail);
console.log(“application dir is ready”);
}
window.requestFileSystem(LocalFileSystem.PERSISTENT, 20*1024*1024 , gotFS, fail); //请求一个文件系统
//return root;
}
function fail(error) {
console.log(“failed to get filesystem:”+error);
}
function dirReady(entry) {
window.appRootDir = entry; //将appRootDir 定义为全局变量
}

2.定义下载方法
function downloadFile(filen,id){
var str = localStorage.getItem(“pdffile”);//这里是判断是否已经下载,用来更新文档下载状态
if(str != null){
if(str.indexOf(filen)+1){
return;
}
}
var fileTransfer = new FileTransfer();
var uri = encodeURI(“http://dev.*****.com.cn/appDownload/”+filen+””);//定义下载的路径
var filePath = window.appRootDir.fullPath+”/”+filen;
navigator.notification.activityStart(“提示”, “数据努力下载中……”); //定义一个下载过度效果
fileTransfer.download(
uri,
filePath,
function(fileEntry) {
navigator.notification.activityStop();
navigator.notification.alert(“下载成功”);
console.log(“download complete: ” + fileEntry.fullPath);
console.log(“download name: ” + fileEntry.name);
var b = localStorage.getItem(“pdffile”);
console.log(“b:”+b);
if(b){
var a = b+”,”+fileEntry.name;
localStorage.setItem(“pdffile”,a);
}else{
localStorage.setItem(“pdffile”,fileEntry.name);
}
$(“#pdf li:eq(“+id+”) a:eq(1)”).text(“已下载”);//更改下载状态
},
function(error) {
navigator.notification.activityStop();
console.log(“download error source ” + error.source);
console.log(“download error target ” + error.target);
console.log(“upload error code” + error.code);
}
);
}

3.定义打开文档方法
function openpdf(ad){
var str = localStorage.getItem(“pdffile”);//判断是否已经下载,如果下载就打开该文档
if(str != null){
if(str.indexOf(ad)+1){
window.plugins.fileOpener.open(“file:///mnt/sdcard/download_pdf/”+ad);
}else{
}
}else{
}
}

4.删除已下载文档
function removefile(filen,id) {
var str = localStorage.getItem(“pdffile”);//判断文档是否存在如果存在就删除。
if(str==null){navigator.notification.alert(filen+’不存在!’);return;}
var newstr=str.replace(filen,””);
localStorage.setItem(“pdffile”,newstr);
console.log(“newstr:”+newstr);
window.appRootDir.getFile(filen, {create: false,exclusive: false},
function(fileEntry){
console.log(“name:”+fileEntry.name);
fileEntry.remove(
function() {
navigator.notification.alert(fileEntry.name+’已删除!’);
$(“#pdf li:eq(“+id+”) a:eq(1)”).text(“下载”);
},
fail);
},
function(error){
navigator.notification.alert(filen+’不存在!’);
console.log(“Error removing file: ” + error.code);
}
);
}
这样几个方法就可以很好实现文档下载,打开阅读,状态更改,删除文件等功能。

致力于最TOP移动广告平台---KeyMob以移动营销为目的的手机广告优化管理平台,致力于为广告主提供精准的产品营销和应用推广服务,为应用开发者创造更高的广告收益。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐 上一条 /1 下一条

ionic4视频教程

Archiver|手机版|小黑屋| PhoneGap中文网 ( 京ICP备13027796号-1 )  

GMT+8, 2024-4-20 21:35 , Processed in 0.042332 second(s), 27 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表