今天用户提了一个需求,需要在APP的我的页面新增一个查看用户手册的功能,在不引入插件的情况下使用如下代码实现:
代码:
//下载用户手册到本地
function downloadFile(){
plus.io.resolveLocalFileSystemURL(
"_downloads/", //指定的文件目录
function(entry){
var directoryReader = entry.createReader();//获取读取目录的对象
directoryReader.readEntries(function(entries){
var existFile = false; //判断是否存在用户手册.pdf文件
//遍历子目录
for (var i = 0; i < entries.length; i++) {
if(entries[i].name == "用户手册.pdf"){
plus.runtime.openFile("_downloads/"+entries[i].name);
existFile = true;
break;
}
}
if(!existFile){
var w;
mui.plusReady(function() {
w = plus.nativeUI.showWaiting("正在下载用户手册,请稍后...", {
back: "none"
});
});
var dtask = plus.downloader.createDownload(
url, //文件下载地址
{
method:"GET",
filename:"_downloads/", //下载文件保存路径
},
function(d, status){
if(status == 200){
//下载成功后打开文件
plus.runtime.openFile(d.filename)
}else{
mui.alert("下载失败,请检查网络后请重试")
}
w.close();
})
dtask.start();
}
},function(err){
mui.toast("访问文件失败")
})
},
function(err){
mui.toast("访问文件失败")
})
}